PHP头条
热点:

php求相对路径的函数


求相对路径的函数,写一个函数,算出两个文件的相对路径如 $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php'; 计算出 $b 相对于 $a 的相路径。

$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';

//求$b相对于$a的相对路径
function getRelativelyPath($a, $b) {
    //拆分成数组
    $a = explode('/', $a);
    $b = explode('/', $b);
    $path = '';

    //将两个数组的索引重置
    $c = array_values(array_diff($a, $b));
    $d = array_values(array_diff($b, $a));

    //去除掉a路径的文件名
    array_pop($c);

    //将a路径中的目录名替换为..
    foreach ($c as &$v) $v = '..';

    //合并两个数组
    $e = array_merge($c, $d);

    //拼接路径
    foreach ($e as &$v) $path .= $v . '/';

    return rtrim($path, '/');
}

www.phpzy.comtrue/php/28802.htmlTechArticlephp求相对路径的函数 求相对路径的函数,写一个函数,算出两个文件的相对路径如 $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php'; 计算出 $b 相对于 $a 的相路径。 $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/...

相关文章

PHP之友评论

今天推荐