PHP Function for URI and URI Segments
Here's a function I wrote to parse through the URI to get segments. I wrote it to use with CodeIgniter to get URI segments before they had been altered and "-" gets changed to "_". It can be used in any application.
PHP:
-
<?php
-
function uri($segment=NULL, $qs=false){
-
$uri = $_SERVER['REQUEST_URI'];
-
if(!$qs || $segment !== NULL){
-
}
-
if($segment !== NULL){
-
if($segment != 'last'){
-
}
-
}
-
} elseif($segment <= $ttl){
-
$seg = $segments[$segment-1];
-
} else {
-
return '';
-
}
-
-
return $seg;
-
}
-
}
-
return $uri;
-
} ?>
Usage:
PHP:
-
// uri: http://www.mysite.com/some-controller/some-method/
-
-
// displays: /some-controller/some-method/
-
-
// 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
-
-
// displays: /some-controller/?foo=bar&bar=foo
December 14th, 2009 at 10:45 am
Nice article thank you for sharing!