Use PHP to calculate the relative paths of the two paths
- 2020-06-15 07:50:40
- OfStack
<html>
<body>
<?php
function relativePath($aPath, $bPath) {
$aArr = explode('/', $aPath); //explode The function is used to slice strings , Returns the sharded array , Here use '/' Split string
$bArr = explode('/', $bPath);
$aDiffToB = array_diff_assoc($aArr, $bArr); //array_diff_assoc() Used to get A An array with the B The difference set of elements between arrays ,Key and Value They're all different as different elements , Return here to A Array and B Different elements in an array
$count = count($aDiffToB);
$path = '';
for($i = 0; $i < $count - 1; $i++){
$path .= '../';
}
$path .= implode('/', $aDiffToB); //implode() Used to concatenate array elements with the specified string , To return to '/' String after concatenating an array element
return $path;
}
echo relativePath('/a/b/c/d/a.php', '/a/b/1/2/b.php');
?>
</body>
</html>
Page output
. ./. ./c/d/a.php