1 function getRealRelativePath($a, $b)
 2 {
 3 
 4     $a_arr = explode('/', $a);
 5     $b_arr = explode('/', $b);
 6     $diff = $this->getDiff($a_arr, $b_arr);
 7     $path = str_repeat('../', count($diff));
 8     $b_diff = $this->getDiff($b_arr, $a_arr);
 9     $path .= implode('/', $b_diff);
10     return $path;
11 }
12 
13 function getDiff($a,$b){
14     $i=0;
15     while (isset($a[$i]) && isset($b[$i]) && $a[$i] == $b[$i]) {
16         unset($a[$i]);
17         $i++;
18     }
19     return $a;
20 }
21 
22 $urla = "/home/web/lib/img/cache.php";
23 $urlb = "/home/web/api/img/temp/show.php";
24 $this->getRealRelativePath($urla,$urlb);
25 //b相对于a的路径为../../../api/img/temp/show.php
26 //不能使用array_diff_assoc和array_intersect_assoc等函数,
27 //因为下标和值一样的路径会存在,如上面的两个路径,会造成数据不正确

 

posted on 2020-07-09 19:04  紫星空  阅读(213)  评论(0)    收藏  举报