Home > Backend Development > PHP Tutorial > php求两个目录的相对路径示例(php获取相对路径)_PHP

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

WBOY
Release: 2016-06-01 11:55:02
Original
966 people have browsed it

求两个目录的相对路径,不限制路径深度
复制代码 代码如下:
/**
 * 输出$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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template