题目:$a = "/a/b/c/d/e.php"; $b = "/a/b/12/23/f.php"; 函数返回$b相对于$a的相对路径?
//获取路径path2相对于path1的路径
function getRelatepath($path1, $path2)
{
$path1Arr = explode('/', $path1);
$path2Arr = explode('/', $path2);
$ins = array_intersect($path1Arr, $path2Arr);
$count = count($path1Arr) - count($ins);
$str = str_repeat('../', $count);
$result = str_replace(implode('/', $ins).'/', $str, $path2);
return $result;
}
$a = "/a/b/c/d/e.php";
$b = "/a/b/12/23/f.php";
echo getRelatepath($a, $b);
结果是:http://www.cnblogs.com/../12/23/f.php