Home >Backend Development >PHP Tutorial >Calculate the relative path of two paths using PHP_PHP Tutorial

Calculate the relative path of two paths using PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:06:181861browse

Copy code The code is as follows:


function relativePath($aPath, $bPath) {
                                                                                                                                                                                                                                                                         Split the string
$bArr = explode('/', $bPath);
$aDiffToB = array_diff_assoc($aArr, $bArr); //array_diff_assoc() is used to get the difference between array A and array B For the difference set of elements, if both Key and Value are different, they are regarded as different elements. Here, the elements in the A array that are different from the B array are returned. PATH = '';
for ($ i = 0; $ i & lt; $ Count -1; $ i ++) {
$ PATH. = '../';
🎜>

                 $path .= implode('/', $aDiffToB); // implode() is used to connect the array elements using the specified string, here returns the string after connecting the array elements with '/'

return $path;
}

echo relativePath('/a/b/c/d/a.php', '/a/b/1/2/b.php');
 /d/a.php





http://www.bkjia.com/PHPjc/327622.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/327622.html
TechArticle
Copy the code code as follows: html body ?php function relativePath($aPath, $bPath) { $aArr = explode( '/', $aPath); //explode function is used to split the string and return the split array. Here...

Statement:
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