两个绝对路径!写个函数,找出两者相对路径(1.php相对于2) //这两个是绝对路径!写个函数,找出两者相对路径(1.php相对于2)$a = "/a/b/c/1.php";$b = "/a/b/f/2.php";function getpathinfo($a, $b){ $a2arra
//这两个是绝对路径!写个函数,找出两者相对路径(1.php相对于2)
$a = "/a/b/c/1.php";
$b = "/a/b/f/2.php";
function getpathinfo($a, $b)
{
$a2array = explode('/', $a);
$b2array = explode('/', $b);
$acount = count($a2array);//计算出a数组的总数
$bcount = count($b2array);//计算出b数组的总数 总共能分出来6个,我们去除第0个和第5个
$pathinfo = "";
for ($i = 1; $i <= $bcount - 2; $i++) {
if ($a2array[$i] != $b2array[$i]) {
for ($m = $i; $m <= $acount - 2; $m++) { //$i=3 $m = 8 $m = 6
$pathinfo .= "../";
//echo $pathinfo;
}
// echo $pathinfo;
// echo "";
if ($pathinfo != "") { //如果不等于空的时候去除/
$pathinfo = rtrim($pathinfo, "/"); //去除当前字符串最后的
}
for ($k = $i; $k <= $bcount - 2; $k++) {
$pathinfo .= "/" . $b2array[$k];
}
break;
}
}
return $pathinfo;
}
echo '';
echo getpathinfo($a, $b);
