note 73563 deleted from function.realpath by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Jun 2009 13:02:04 +0000 |
| Subject: | note 73563 deleted from function.realpath by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156237@lists.php.net to get a copy of this message | ||
Note Submitter:
----
Tyron Madlener said "I just wonder why all the other solutions are so complex and that mine so
simple".
Funny. I wonder why yours is so complex (26 lines, 2 functions) when it can be done in half that
(13 lines, 1 function):
function rp($p) {
$p=explode('/', $p);
$o=array();
for ($i=0; $i<sizeof($p); $i++) {
if (''==$p[$i] || '.'==$p[$i]) continue;
if ('..'==$p[$i] && $i>0 && '..'!=$o[sizeof($o)-1]) {
array_pop($o);
continue;
}
array_push($o, $p[$i]);
}
return implode('/', $o);
}
With that, this:
print rp("../../xyz.txt")."\n";
print rp("a/b/c/d/../../xyz.txt")."\n";
print rp("a/b/c/d/../..//xyz.txt")."\n";
Prints:
../../xyz.txt
a/b/xyz.txt
a/b/xyz.txt
As expected.