note 77203 added to function.realpath
| From: | SantoshPatnaik at osu1 dot php dot net | Date: | Mon, 20 Aug 2007 09:52:24 +0000 |
| Subject: | note 77203 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129631@lists.php.net to get a copy of this message | ||
Given real paths of two files, this function finds the relative path of one ($dest) with respect to
the other ($root).
echo rel_path('a/b/c', 'd/e'); // './../../a/b/c'
echo rel_path('a/b/c', 'a/b/c/d/e'); // './../..'
echo rel_path('a/b/c/d/e', 'a/b/c'); // './d/e'
function rel_path($dest, $root = '', $dir_sep = '/')
{
$root = explode($dir_sep, $root);
$dest = explode($dir_sep, $dest);
$path = '.';
$fix = '';
$diff = 0;
for($i = -1; ++$i < max(($rC = count($root)), ($dC = count($dest)));)
{
if(isset($root[$i]) and isset($dest[$i]))
{
if($diff)
{
$path .= $dir_sep. '..';
$fix .= $dir_sep. $dest[$i];
continue;
}
if($root[$i] != $dest[$i])
{
$diff = 1;
$path .= $dir_sep. '..';
$fix .= $dir_sep. $dest[$i];
continue;
}
}
elseif(!isset($root[$i]) and isset($dest[$i]))
{
for($j = $i-1; ++$j < $dC;)
{
$fix .= $dir_sep. $dest[$j];
}
break;
}
elseif(isset($root[$i]) and !isset($dest[$i]))
{
for($j = $i-1; ++$j < $rC;)
{
$fix = $dir_sep. '..'. $fix;
}
break;
}
}
return $path. $fix;
}
----
Server IP: 203.199.124.111
Probable Submitter: 85.195.119.14
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- https://master.php.net/note/edit/77203
Del: integrated -- https://master.php.net/note/delete/77203/integrated
Del: useless -- https://master.php.net/note/delete/77203/useless
Del: bad code -- https://master.php.net/note/delete/77203/bad+code
Del: spam -- https://master.php.net/note/delete/77203/spam
Del: non-english -- https://master.php.net/note/delete/77203/non-english
Del: in docs -- https://master.php.net/note/delete/77203/in+docs
Del: other reasons-- https://master.php.net/note/delete/77203
Reject -- https://master.php.net/note/reject/77203
Search -- https://master.php.net/manage/user-notes.php