Home  >  Article  >  Backend Development  >  Use PHP to find the relative paths of two files_PHP tutorial

Use PHP to find the relative paths of two files_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:05:161113browse

复制代码 代码如下:

function compare($ph1,$ph2){
    $ret = '';
    $_f1Arr = explode("/",$ph1);
    $_f2Arr = explode("/",$ph2);

    $f1 = array_pop($_f1Arr);
    $f2 = array_pop($_f2Arr);
    for($i=0;$i        if($_f1Arr[$i] !== $_f2Arr[$i])
            break;
    }
    for($j= $i-1;$j       $ret .= "../";
    }

    for($i-1;$i       $ret .= $_f2Arr[$i].'/';
    }
    return $ret.$f2;
}
$file1 = "aaa/ddd/ccc/ddd/test/a.js";
$file2 = "aaa/ddd/ccc/ddd/test/b.js";
echo compare($file1,$file2);
?>

B对于A的相对路径。。。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327723.htmlTechArticle复制代码 代码如下: function compare($ph1,$ph2){ $ret = ''; $_f1Arr = explode("/",$ph1); $_f2Arr = explode("/",$ph2); $f1 = array_pop($_f1Arr); $f2 = array_pop($_f2Arr); for...
Statement:
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