ShoeLace1291 Posted January 17, 2011 Share Posted January 17, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/224670-array-values-after-key/ Share on other sites More sharing options...
QuickOldCar Posted January 17, 2011 Share Posted January 17, 2011 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 />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/224670-array-values-after-key/#findComment-1160527 Share on other sites More sharing options...
ShoeLace1291 Posted January 17, 2011 Author Share Posted January 17, 2011 Hmm ok maybe I wasn't clear enough, but how would I get an array like that to print an array like this: array('blog', 'view', 1); How wouold I get that to return as an array like this: array(1) Quote Link to comment https://forums.phpfreaks.com/topic/224670-array-values-after-key/#findComment-1161020 Share on other sites More sharing options...
BlueSkyIS Posted January 17, 2011 Share Posted January 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224670-array-values-after-key/#findComment-1161030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.