Jump to content

Array Values After Key


ShoeLace1291

Recommended Posts

I'm trying to split a url into segments so that a url would be stored in an array. 

 

Example:

A url like this http://localhost/application/blog/view/Test-Blog-Entry/1

 

Would look like this in an array:

array('blog', 'post', 'Test-Blog-Entry', '1');

 

It's an MVC url so 'blog' would be controller, 'post', would be method and the last two would be variables.  I want to display the variable values of the array.

 

Is there an easy way to do this?

Link to comment
Share on other sites

You can break this down to something more specific to your needs as a function.

 

like this:

<?php
$url = "http://localhost/application/blog/view/Test-Blog-Entry/1";

function explodePaths($new_parse_url) {        
            $get_path_parse_url = parse_url($new_parse_url, PHP_URL_PATH);                     
            $path_parts = explode("/",$get_path_parse_url);

            RETURN $path_parts;
}                   

echo "url: $url<br />";
$paths = explodePaths($url);          
print_r($paths);

?>

this returns:

Array ( [0] => [1] => application [2] => blog [3] => view [4] => Test-Blog-Entry [5] => 1 )

 

 

or since I have the complete you can have this too if need it:

<?php
$url = "http://localhost/application/blog/view/Test-Blog-Entry/1";

function getparsedHost($new_parse_url) {
                $parsedUrl = parse_url(trim($new_parse_url));
                return trim($parsedUrl[host] ? $parsedUrl[host] : array_shift(explode('/', $parsedUrl[path], 2)));
} 
$new_parse_url = $url;            
            $get_parse_url = parse_url($new_parse_url, PHP_URL_HOST);
            $host_parse_url .= str_replace(array('Www.','WWW.'), '', $get_parse_url);
            $host_parse_url = strtolower($host_parse_url);
            $port_parse_url = parse_url($new_parse_url, PHP_URL_PORT);
            $user_parse_url = parse_url($new_parse_url, PHP_URL_USER);
            $pass_parse_url = parse_url($new_parse_url, PHP_URL_PASS);
            $get_path_parse_url = parse_url($new_parse_url, PHP_URL_PATH);                     
            $path_parts = explode("/",$get_path_parse_url);
            $extract_path_1 = $path_parts[1];
            $extract_path_2 = $path_parts[2];
            $extract_path_3 = $path_parts[3];
            $extract_path_4 = $path_parts[4];
            $extract_path_5 = $path_parts[5];
            $path_parse_url .= str_replace(array('Www.','WWW.'), '', $get_path_parse_url);
            $query_parse_url = parse_url($new_parse_url, PHP_URL_QUERY);
            $query_parse_url = "?$query_parse_url";
            $query_parse_url = rtrim($query_parse_url, '#');
            $fragment_parse_url = parse_url($new_parse_url, PHP_URL_FRAGMENT);
            $fragment_parse_url = "#$fragment_parse_url";
            $hostpath_url = "$host_parse_url$path_parse_url";
            $hostpath_url = rtrim($hostpath_url, '?');
            $query_parse_url = rtrim($query_parse_url, '?');

            $hostpathquery_url = "$host_parse_url$path_parse_url$query_parse_url";

            $complete_url = "$host_parse_url$port_parse_url$user_parse_url$pass_parse_url$path_parse_url$query_parse_url$fragment_parse_url";
            $complete_url = rtrim($complete_url, '#');
//adding http back to front, can do substr checks in url to suit your needs
$complete_url = "http://$complete_url";        

$parsed = getparsedHost($new_parse_url);          
echo "url: $url<br />";
echo "parsed: $parsed <br />";
echo "host parse: $host_parse_url<br />";
echo "path parse: $path_parse_url<br />";
echo "extract path 1: $extract_path_1<br />";
echo "extract path 2: $extract_path_2<br />";
echo "extract path 3: $extract_path_3<br />";
echo "extract path 4: $extract_path_4<br />";
echo "extract path 5: $extract_path_5<br />";
echo "host and path: $hostpath_url<br />";
echo "complete: $complete_url<br />";

?>

Link to comment
Share on other sites

you could just remove the first two variables, or grab them off the front of the array

 

// (continuing from QuickOldCar's post)
$paths = explodePaths($url);

$controller = array_shift($paths); // Pull off the first value
$method = array_shift($paths); // Pull off the next value

print_r($paths); // should only have the remaining array items.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.