I'm not against regex, but I would have done this instead:
function simplify_path($path, $directory_separator = "/", $equivalent = true){
$path = trim($path);
// if it's absolute, it stays absolute:
$prepend = (substr($path,0,1) == $directory_separator)?$directory_separator:"";
$path_array = explode($directory_separator, $path);
if($prepend) array_shift($path_array);
$output = array();
foreach($path_array as $val){
if($val != '..' || ((empty($output) || $last == '..') && $equivalent)) {
if($val != '' && $val != '.'){
array_push($output, $val);
$last = $val;
}
} elseif(!empty($output)) {
array_pop($output);
}
}
return $prepend.implode($directory_separator,$output);
}
Tests:
echo(simplify_path("../../../one/no/no/../../two/no/../three"));
// => ../../../one/two/three
echo(simplify_path("/../../one/no/no/../../two/no/../three"));
// => /../../one/two/three
echo(simplify_path("/one/no/no/../../two/no/../three"));
// => /one/two/three
echo(simplify_path(".././../../one/././no/./no/../../two/no/../three"));
// => ../../../one/two/three
echo(simplify_path(".././..///../one/.///./no/./no/../../two/no/../three/"));
// => ../../../one/two/three
I thought that it would be better to return an equivalent string, so I respected the ocurrences of ..
at the begining of the string.
If you dont want them, you can call it with the third parameter $equivalent = false:
echo(simplify_path("../../../one/no/no/../../two/no/../three", "/", false));
// => one/two/three
echo(simplify_path("/../../one/no/no/../../two/no/../three", "/", false));
// => /one/two/three
echo(simplify_path("/one/no/no/../../two/no/../three", "/", false));
// => /one/two/three
echo(simplify_path(".././../../one/././no/./no/../../two/no/../three", "/", false));
// => one/two/three
echo(simplify_path(".././..///../one/.///./no/./no/../../two/no/../three/", "/", false));
// => one/two/three
.
? your solution seems to exclude it – Wheatear