So lets say I have a link like this:
http://mywebsite.com/profile/alexkvazos/
I would like to explode the url and get this:
$uri = array('profile','alexkvazos');
What is the easiest approach to this?
So lets say I have a link like this:
http://mywebsite.com/profile/alexkvazos/
I would like to explode the url and get this:
$uri = array('profile','alexkvazos');
What is the easiest approach to this?
try
// if the url is http://www.example.com/foo/bar/wow
function getUriSegments() {
return explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
}
print_r(getUriSegments()); //returns array(0=>'foo', 1=>'bar', 2=>'wow')
Source :- http://www.timwickstrom.com/server-side-code/php/php-get-uri-segments/
Use parse_url()
$path_parts = parse_url('http://mywebsite.com/profile/alexkvazos/');
$uri = explode('/',trim($path_parts['path'],'/'));
print_r($uri);
You could use this piece of code:
<?php
$url = 'http://mywebsite.com/profile/alexkvazos/';
$url = rtrim($url,'/'); // lose the trailing slash
$urlArr = explode("/", $url );
$urlArr = array_reverse( $urlArr );
echo $urlArr[0];
?>
I wouldn't use parse_url
, I would just use preg_split
like this:
$segments = preg_split('#/#', $_SERVER['REQUEST_URI']);
$final = array();
foreach($segments as $key => $value) {
if($value != '') {
$final[] = $value;
}
}
var_dump($final);