<?php
$a = "/a/c/d/a.php";
$b = "/a/c/b.php";
function get_relative($a, $b)
{
$path = '';
$arr_a = explode('/', $a);
$arr_b = explode('/', $b);
$count_a = count($arr_a) - 1;
$count_b = count($arr_b) - 1;
$cur = 0;
while ($cur < $count_a && $cur < $count_b) {
if ($arr_a[$cur] != $arr_b[$cur]) {
break;
}
$cur++;
}
for ($i=$count_b;$i>=0;$i--) {
if ($i == $cur) {
break;
}
$path .= '../';
}
for ($i=$cur;$i<=$count_a;$i++) {
$path .= sprintf("%s/", $arr_a[$i]);
}
$path = rtrim($path, '/');
return $path;
}
echo get_relative($a, $b);
?>