如 $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php'; 计算出 $b 相对于 $a 的相对路径应该是 ../../c/d将()添上
1 回答
梦里花落0921
TA贡献1772条经验 获得超6个赞
function getRelativePath($a, $b) {
$returnPath = array(dirname($b));
$arrA = explode('/', $a);
$arrB = explode('/', $returnPath[0]);
for ($n = 1, $len = count($arrB); $n < $len; $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);
- 1 回答
- 0 关注
- 541 浏览
添加回答
举报
0/150
提交
取消