This article describes how to use PHP to calculate the relative paths of two paths. For more information, see
The code is as follows:
Function relativePath ($ aPath, $ bPath ){
$ Shard R = explode ('/', $ aPath); // The explode function is used to split a string and return the split array. here, the string is split '/'.
$ BArr = explode ('/', $ bPath );
$ ADiffToB = array_diff_assoc ($ region R, $ bArr); // array_diff_assoc () is used to obtain the difference set of elements between array A and array B. different keys and values are considered different elements, elements in array A and different from array B are returned here.
$ Count = count ($ aDiffToB );
$ Path = '';
For ($ I = 0; $ I <$ count-1; $ I ++ ){
$ Path. = '../';
}
$ Path. = implode ('/', $ aDiffToB); // implode () is used to connect an array element with a specified string. here, the string after the array element is connected with '/' is returned.
Return $ path;
}
Echo relativePath ('/a/B/c/d/a. php','/a/B/1/2/B. php ');
?>
Page output
../C/d/a. php