Reputation: 11
With a URI such as /test/test/test
, I would like a way to create an array from $__SERVER['request_uri']
such as this:
[0] => '/' [1] => /test/ [2] => /test/test/ [3] => /test/test/test/
Any help would be appreciated!
Upvotes: 0
Views: 467
Reputation: 10339
A quick an dirty script I just created for you:
<?
$path = "/test/test2/test3";
$arr = explode("/", $path);
$arraynew = array();
$i=0;
foreach ($arr as $k => $v) {
if (($k==0) && ($v=="")) {
$arraynew[$i] = "/";
$i++;
continue;
}
if ($i == 1) {
$arraynew[$i] = $arraynew[$i-1] . $v;
} else {
$arraynew[$i] = $arraynew[$i-1] . "/" . $v;
}
$i++;
}
print_r($arraynew);
?>
Or this one that's more clean and simple and adds "/" at the end:
<?
$path = "/test/test2/test3";
$arr = explode("/", $path);
$arraynew = array();
$i=0;
foreach ($arr as $k => $v) {
$i > 0 ? $arraynew[] = $arraynew[$i-1] . $v . "/" : $arraynew[] = "/";
$i++;
}
print_r($arraynew);
?>
Upvotes: 1
Reputation: 168705
Look up the dirname()
function.
It gives you the parent directory of any given filename or directory name.
In your case, simply use it repeatedly (in a loop) to get each successive parent folder name.
Upvotes: 0