Created
March 7, 2012 21:40
-
-
Save Edwin-Luijten/1996418 to your computer and use it in GitHub Desktop.
PHP : Get url segment
This file contains 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
/** | |
*Get url segment | |
*@param int | |
*@return string [name of requested url segment] | |
**/ | |
function getSegment((int)$segmentPart) | |
{ | |
if(is_int($segmentPart)) | |
{ | |
//Full path | |
$path = trim($_SERVER['REQUEST_URI'], '/'); | |
//Create array of segments from full path | |
$segment = explode('/', $path); | |
//Check if $segment[$segmentPart] is set else it gives undefined offset with no segments in the url | |
if(isset($segment[$segmentPart])) | |
{ | |
return $segment[$segmentPart]; | |
} | |
else | |
{ | |
throw new Exception('$segment[$segmentPart] not set.'); | |
} | |
} | |
else | |
{ | |
throw new Exception('Parameter must be an integer.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment