//在网上搜了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/../../c/d/e.php
$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);
//输出结果:/a/b/12/34/../../c/d/e.php
echo "<hr/>";
function getRelativePath1($a,$b){
$a_url = array(dirname($a));
$b_url = array(dirname($b));
$a_arr = explode('/',$a_url[0]);
$b_arr = explode('/',$b_url[0]);
$len = count($a_arr);
$art1 = '';
$art2 = '';
for($i=1;$i<$len;$i++){
if($a_arr[$i]<>$b_arr[$i]){
$art1.= '../';
$art2.= $a_arr[$i].'/';
}
}
return $art1.$art2;
}
echo getRelativePath1($a, $b);
//输出结果:../../c/d/
echo "<hr/>";
function getpathinfo( $a, $b ) {
$a2array = explode('/', $a);
$b2array = explode('/', $b);
$pathinfo = '';
for( $i = 1; $i <= count($b2array)-2; $i++ ) {
$pathinfo.=$a2array[$i] == $b2array[$i] ? '../' : $b2array[$i].'/';
}
print_R($pathinfo);
}
getpathinfo($a, $b);
//输出结果:../../12/34/
echo "<hr/>";
$path="";
$path1 = explode('/',dirname($a));
$path2 = explode('/',dirname($b));
$aLen = count($path1);
$bLen = count($path2);
$maxLen = max($aLen,$bLen);
for($i = 1; $i < $maxLen; $i++){
if($path1[$i] != $path2[$i] && isset($path1[$i])){
if(isset($path2[$i]))$bUrl[]=$path2[$i];
$path.= "../";
}else{
$bUrl[]=$path2[$i];
}
}
echo $path.implode('/',$bUrl).'/'.basename($b);
//输出结果:../../a/b/12/34/c.php
?>
echo "写轮眼"
echo "轮回眼";
echo "楼主你好";
第三种方法不对的,
$a = '/a/b/12/c/d/e.php';
$b = '/a/b/12/34/c.php';