<?php function releative_path($path1,$path2){ $arr1 = explode("/",dirname($path1)); $arr2 = explode("/",dirname($path2)); for ($i=0,$len = count($arr2); $i < $len; $i++) { if ($arr1[$i]!=$arr2[$i]) { break; } } // 不在同一个根目录下 if ($i==1) { $return_path = array(); } // 在同一个根目录下 if ($i != 1 && $i < $len) { $return_path = array_fill(0, $len - $i,".."); } // 在同一个目录下 if ($i == $len) { $return_path = array('./'); } $return_path = array_merge($return_path,array_slice($arr1,$i)); return implode('/',$return_path); } $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php'; $c = '/e/b/c/d/f.php'; $d = '/a/b/c/d/g.php'; echo releative_path($a,$b);//结果是../../c/d echo "<br />"; echo releative_path($a,$c);//结果是a/b/c/d echo "<br />"; echo releative_path($a,$d);//结果是./ echo "<br />"; ?>
更多:http://www.shanhubei.com/archives/55200.html
标签:12,..,echo,相对路径,path,php,releative From: https://www.cnblogs.com/shanhubei/p/18129314