<?php
function compara_path($path_a, $path_b) {
// 切割路径.
$array_a = explode('/', $path_a);
$array_b = explode('/', $path_b);
// 从数组中删除最后的文件,剩下的全为文件夹名称.
$file_a = array_pop($array_a);
$file_b = array_pop($array_b);
// 子目录个数.
$a_len = count($array_a);
$b_len = count($array_b);
// 循环求出第几个目录不同.
for ( $i = 0; $i < $a_len; $i++ ) {
if ( $array_a[$i] != $array_b[$i] ) {
break;
}
}
// 求出相对路径.
$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;
echo $com_path;
}
$path_a = "/home/a/b/c/d/xxx.txt";
$path_b = "/home/a/b/e/i/h/e/f/xxxxx.txt";
compara_path($path_a, $path_b);
?>
function compara_path($path_a, $path_b) {
}
$path_a = "/home/a/b/c/d/xxx.txt";
$path_b = "/home/a/b/e/i/h/e/f/xxxxx.txt";
compara_path($path_a, $path_b);
?>