-
-
Save carlosh1989/20b9c4e8d418bc522cd22dcc31bbb330 to your computer and use it in GitHub Desktop.
Function to parse URI segments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function uri($segment=NULL, $qs=false){ | |
$uri = $_SERVER['REQUEST_URI']; | |
if(!$qs || $segment !== NULL){ | |
if(strpos($uri, '?')){ | |
list($uri, $query) = explode('?', $uri); | |
} | |
if($segment !== NULL){ | |
if(is_string($segment)){ | |
if($segment != 'last'){ | |
return ( strlen($uri) >= strlen($segment) && substr($uri, 0, strlen($segment)) ); | |
} | |
} | |
$str = trim($uri, '/'); | |
$segments = (strpos($str, '/')) ? explode('/', $str) : array($str); | |
$ttl = count($segments); | |
if(is_string($segment) && $segment == 'last'){ | |
$seg = array_pop($segments); | |
} elseif($segment <= $ttl){ | |
if($segment < 0) $segment = $ttl - abs($segment) + 1; | |
$seg = $segments[$segment-1]; | |
} else { | |
return ''; | |
} | |
return $seg; | |
} | |
} | |
return $uri; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// uri: http://www.mysite.com/some-controller/some-method/ | |
echo uri(); | |
// displays: /some-controller/some-method/ | |
echo uri(2); | |
// displays: some-method | |
// Pass true into 2nd paramater ($qs) to display Query String with URI | |
// uri: http://www.mysite.com/some-controller/?foo=bar&bar=foo | |
echo uri(NULL, true); | |
// displays: /some-controller/?foo=bar&bar=foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment