note 55825 deleted from function.realpath by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Jun 2009 13:03:07 +0000 |
| Subject: | note 55825 deleted from function.realpath by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156241@lists.php.net to get a copy of this message | ||
Note Submitter: Tyron Madlener
----
None of the functions provided here worked for me. All of them somehow had the problem with pathes
like '../../downloads/bar/../foo/..' or didn't provide relative paths.
So I coded my one that justs removes '.'-dirs from the paths and simplifies stuff like
'dir/..'
function SimplifyPath($path) {
$dirs = explode('/',$path);
for($i=0; $i<count($dirs);$i++) {
if($dirs[$i]=="." || $dirs[$i]=="") {
array_splice($dirs,$i,1);
$i--;
}
if($dirs[$i]=="..") {
$cnt = count($dirs);
$dirs=Simplify($dirs, $i);
$i-= $cnt-count($dirs);
}
}
return implode('/',$dirs);
}
function Simplify($dirs, $idx) {
if($idx==0) return $dirs;
if($dirs[$idx-1]=="..") Simplify($dirs, $idx-1);
else array_splice($dirs,$idx-1,2);
return $dirs;
}
I just wonder why all the other solutions are so complex and that mine so simple, because in general
the problem of simplifieng isn't that hard to solve. Well, I hope that there aren't any
cases on which my solution fails. Otherwise, feel free to improve :D