Find the relative paths of two files in php_PHP tutorial

WBOY
Release: 2016-07-13 17:46:21
Original
840 people have browsed it

//Use php to find the relative paths of two files
function compara_path($path_a, $path_b) {
//Cutting path.
$array_a =explode('/', $path_a);
$array_b =explode('/', $path_b);
//Delete the last file from the array, and the remaining files are all folder names.
$file_a =array_pop($array_a); //array_pop() pops and returns the last element of the array
$file_b =array_pop($array_b);
//Number of subdirectories.
$a_len =count($array_a);
$b_len =count($array_b);
//Loop to find out which directories are different.
for ( $i =0; $i < $a_len; $i++ ) {
if ($array_a[$i] != $array_b[$i] ) {
        break;
}
}
//Find the relative path.
$com_path="";
for ( $j =0; $j < $a_len - $i; $j++ ) {
$com_path .='../';
}
for ( $i; $i< $b_len; $i++ ) {
$com_path .=$array_b[$i] . '/';
}
$com_path .=$file_b;
return $com_path;
}
$path_a = "a/b/c/d/e/f.php";
$path_b = "a/b/z/x/y.php";
echo compara_path($path_a, $path_b);

Excerpted from: Xiao囧’s blog

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478585.htmlTechArticle?php //Use php to find the relative paths of two files function compara_path($path_a, $path_b) { / /Cut path. $array_a =explode(/, $path_a); $array_b =explode(/, $path_b); //Remove from array...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!