//在网上搜了4种答案,结果都不一样,请问哪个是对的?还是都不对,您有好的解决办法吗?求//解!!! ?php header("content-type:text/html;charset=utf-8"); $a='/a/b/c/d/e.php'; $b='/a/b/12/34/c.php'; function getRelativePath($a, $b) {///a/b/12/34/..
//在网上搜了4种答案,结果都不一样,请问哪个是对的?还是都不对,您有好的解决办法吗?求//解!!!
header("content-type:text/html;charset=utf-8");
$a='/a/b/c/d/e.php';
$b='/a/b/12/34/c.php';
function getRelativePath($a, $b) {///a/b/12/34/../../c/d/e.php
$returnPath = array(dirname($b));
$arrA = explode('/', $a);
$arrB = explode('/', $returnPath[0]);
for ($n = 1, $len = count($arrB); $n if ($arrA[$n] != $arrB[$n]) {
break;
}
}
if ($len - $n > 0) {
$returnPath =array_merge($returnPath, array_fill(1, $len - $n, '..'));
}
$returnPath = array_merge($returnPath,array_slice($arrA, $n));
return implode('/', $returnPath);
}
echo getRelativePath($a, $b);
//输出结果:/a/b/12/34/../../c/d/e.php
echo "