php 算法之实现相对路径的实例

php 算法之实现相对路径的实例

算出相对路径(相同的目录可以忽略用../ 或者 ./ 表示)

实现代码:

class Relatively{ private function __construct(){ } /** * 算出相对路径(相同的目录可以忽略用../ 或者 ./ 表示) * @param Strint $path1 * @param Strint $path2 * @return string */ public static function relaroot($path1,$path2){ $rearray=array(); $arr1=explode('https://www.jb51.net/', dirname($path1)); $arr2=explode('https://www.jb51.net/', dirname($path2)); for($i=0,$len=count($arr2)-1;$i<$len;$i++){ if($arr1[$i]!=$arr2[$i]){ break; } if($i==1){ $rearray=array(); } if($i!=1 && $i<$len){ $rearray=array_fill(0,$len-$i,'..'); } if($i==$len){ $rearray=array('./'); } } $reroot=array_merge($rearray,array_slice($arr2, $i)); return implode('https://www.jb51.net/', $reroot); } } $path1="a/b/c/d/index.php"; $path2="/a/b/12/34/index1.php"; $a=Relatively::relaroot($path1, $path2); echo $a;

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/169078643258d4f4e5a296583ff64800.html