復(fù)制代碼 代碼如下:
<html>
<body>
<?php
function relativePath($aPath, $bPath) {
$aArr = explode('/', $aPath); //explode函數(shù)用于切分字符串,返回切分后的數(shù)組,此處用'/'切分字符串
$bArr = explode('/', $bPath);
$aDiffToB = array_diff_assoc($aArr, $bArr); //array_diff_assoc()用于獲取A數(shù)組與B數(shù)組之間元素的差集,Key和Value都不相同視為不同元素,此處返回在A數(shù)組中且與B數(shù)組不相同的元素
$count = count($aDiffToB);
$path = '';
for($i = 0; $i < $count - 1; $i++){
$path .= '../';
}
$path .= implode('/', $aDiffToB); //implode()用于使用指定字符串連接數(shù)組元素,此處返回用'/'連接數(shù)組元素后的字符串
return $path;
}
echo relativePath('/a/b/c/d/a.php', '/a/b/1/2/b.php');
?>
</body>
</html>
新聞熱點(diǎn)
疑難解答