一个路径相对于另一个路径
$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
function getRelativePath($a, $b) {
$returnPath = array(dirname($b));
$arrA = explode('/', $a);
$arrB = explode('/', $returnPath[0]);
$len = count($arrB);
for ($n = 1; $n < $len; $n++) {
if ($arrA[$n] != $arrB[$n]) {
break;
}
}
if ($len - $n > 0) {
$returnPath = array_merge(array_fill(1, $len - $n, '..'), array_slice($arrA, $n));
return implode('/', $returnPath);
}
}
echo getRelativePath($a, $b);
$b = '/a/b/12/34/c.php';
function getRelativePath($a, $b) {
$returnPath = array(dirname($b));
$arrA = explode('/', $a);
$arrB = explode('/', $returnPath[0]);
$len = count($arrB);
for ($n = 1; $n < $len; $n++) {
if ($arrA[$n] != $arrB[$n]) {
break;
}
}
if ($len - $n > 0) {
$returnPath = array_merge(array_fill(1, $len - $n, '..'), array_slice($arrA, $n));
return implode('/', $returnPath);
}
}
echo getRelativePath($a, $b);