note 36726 deleted from function.realpath by aidan
| From: | aidan@php.net | Date: | Sun, 29 Aug 2004 21:50:20 +0000 |
| Subject: | note 36726 deleted from function.realpath by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75606@lists.php.net to get a copy of this message | ||
Note Submitter: mail at mariocarbonell dot com
----
After having tested Jose Miguel resolvePath() and seeing that doesn't works perfect, i've
rewrite my own version using regular expresions.
// This funcion performs ereg_replace iteratively until no changes is made.
function iterate_ereg_replace ( $pattern, $replacement, $string) {
$result = $string;
do {
$string = $result;
$result = ereg_replace ($pattern, $replacement, $string);
} while ($result != $string);
return $result;
}
// This funcion performs the translation using regular expresions.
function resolvePath2 ($dest, $orig) {
if ($dest == "") return $orig;
if ($orig == "") return $dest;
if ($dest[0] == "/") return $dest;
if ($orig[0] != "/") $orig = "/".$orig;
if ($orig[strlen($orig)-1] != "/") $orig = dirname($orig)."/";
$path = $orig.$dest;
$path = ereg_replace ("/+", "/", $path);
$path = iterate_ereg_replace ("/\./", "/", $path);
$path = iterate_ereg_replace ("/[^(/|\.)]+/\.\./", "/", $path);
return $path;
}
Mario Carbonell.
http://www.mariocarbonell.com