note 101471 deleted from function.realpath by danbrown
| From: | danbrown@php.net | Date: | Sun, 19 Dec 2010 17:22:25 +0000 |
| Subject: | note 101471 deleted from function.realpath by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175064@lists.php.net to get a copy of this message | ||
Note Submitter: mail at miikku dot de
----
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))), '/'));
}
?>