PHP练习-计算两个文件的相对路径

2020-09-02 19:54 By "Powerless" 2665 0 2

思路分析

从两个路径不同处开始用../填充之后的路径填充完毕后拼接B文件名称(不考虑不同目录)

function filePath($file1,$file2)
{
    $f1 = explode('/',$file1);
    $f2 = explode('/',$file2);
    $path = '';
    foreach ($f1 as $k => $v){
        if(!array_key_exists($k,$f2)){
            $path .= '../';
        }
        if(strspn('.',$v)){
            $path .= end($f2);
        }
    }
    return $path;
}
$f1 = 'a/b/c/d/e.php';
$f2 = 'a/b/c/f.php';
echo filePath($f1,$f2);

输出结果:../f.php

评 论

Others Discussion

  • 初识七层、五层、四层网络协议
    Posted on 2021-04-09 16:52
  • Redis各种数据类型的使用场景举例分析【二】
    Posted on 2018-11-22 10:30
  • QPS、TPS、RT、吞吐量到底是什么
    Posted on 2020-02-02 01:15
  • 2018年云计算热词
    Posted on 2019-06-12 18:19
  • Linux工具 - NM目标文件格式分析
    Posted on 2019-04-24 10:29
  • ACID原则
    Posted on 2020-12-17 16:36
  • 前端知识体系精简-Css
    Posted on 2018-03-28 18:34
  • 浏览器访问网站经历的步骤-Html
    Posted on 2018-11-28 18:48