Home > php教程 > php手册 > php求两个目录的相对路径示例(php获取相对路径)

php求两个目录的相对路径示例(php获取相对路径)

WBOY
Release: 2016-06-06 20:23:42
Original
1703 people have browsed it

这篇文章主要介绍了php求两个目录的相对路径示例(php获取相对路径),需要的朋友可以参考下

求两个目录的相对路径,不限制路径深度

复制代码 代码如下:


/**
 * 输出$b相对于$a的相对路径($a)
 * 不限限制路径深度,,没有做什么优化,只是实现功能
 */
function getPath($a, $b)
{
 $aArr = explode('/', dirname($a));
 $bArr = explode('/', dirname($b));

 $aLen = count($aArr);
 $bLen = count($bArr);

 $len = max($aLen, $bLen);

 $k = 0;

 for($i = 0; $i  {
 if($k == 0)
 {
 if(isset($aArr[$i]) && ($aArr[$i] != $bArr[$i]))
 {
 $d .= '../';

 if(isset($bArr[$i]))
 {
 $nP[$i] = $bArr[$i];
 }

 $k = $k + 1;
 }
 }
 else
 {
 if(isset($aArr[$i]))
 {
 $d .= '../';
 }
 if(isset($bArr[$i]))
 {
 $nP[$i] = $bArr[$i];
 }
 }
 }

 echo $d.implode('/', $nP);
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template