//写一个函数,算出两个文件的相对路径如$a=’/a/b/c/d/e.php’; $b=’/a/b/12/34/c.php’;
//计算出$b相对于$a的相对路径应该是:../../c/d
function path($a,$b){
$astr=trim(dirname($a),'/');
$bstr=trim(dirname($b),'/');
$aarr=explode('/',$astr);
$barr=explode('/',$bstr);
$adiff=array_diff($aarr,$barr);
$bdiff=array_diff($barr,$aarr);
$bnum=count($bdiff);
$bpath=str_repeat('../',$bnum);
$apath=implode('/',$adiff);
$path=$bpath.$apath;
return $path;
}
$a='/a/b/c/d/e.php';
$b='/a/b/12/34/c.php';
echo path($a,$b);
阅读(905) | 评论(0) | 转发(0) |