note 101471 added to function.realpath
| From: | mail at miikku dot de | Date: | Sat, 18 Dec 2010 23:17:07 +0000 |
| Subject: | note 101471 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175048@lists.php.net to get a copy of this message | ||
Just as "jpic" I did search exactly the opposite of "realpath()" and didn't
find anything implemented.
So I tried his code but it fails if two directories have the same name but aren't contained in
the same parent-directory (i.e. getRelativePath("/some/directory",
"/another/directory") does return "../some" which is obviously incorrect).
Besides his code isn't really effective. So here is my fixed, smaller and (on average) 4.8
times faster code:
<?php
function relpath($sPath, $sBase){
$sPath = trim($sPath, '/');
$aBase = explode('/', trim($sBase, '/'));
$aReturn = array();
while(strpos($sPath.'/', implode('/', $aBase).'/') !== 0 &&
array_pop($aBase) !== NULL) $aReturn[] = '..';
$sStaticPart = empty($aReturn) ? '' : implode('/', $aReturn).'/';
return($sStaticPart.trim(substr($sPath, strlen(implode('/', $aBase))), '/'));
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 93.219.190.244
----
Manual Page -- http://www.php.net/manual/en/function.realpath.php
Edit -- https://master.php.net/note/edit/101471
Del: integrated -- https://master.php.net/note/delete/101471/integrated
Del: useless -- https://master.php.net/note/delete/101471/useless
Del: bad code -- https://master.php.net/note/delete/101471/bad+code
Del: spam -- https://master.php.net/note/delete/101471/spam
Del: non-english -- https://master.php.net/note/delete/101471/non-english
Del: in docs -- https://master.php.net/note/delete/101471/in+docs
Del: other reasons-- https://master.php.net/note/delete/101471
Reject -- https://master.php.net/note/reject/101471
Search -- https://master.php.net/manage/user-notes.php