Home >php教程 >php手册 >PHP source code calculates the relative paths of two files

PHP source code calculates the relative paths of two files

WBOY
WBOYOriginal
2016-10-28 15:03:261391browse
<?<span style="color: #000000">php
</span><span style="color: #008000">//</span><span style="color: #008000">计算出两个文件的相对路径即path2相对于$path1的相对路径
// http://www.manongjc.com/article/1342.html</span>
<span style="color: #0000ff">function</span> get_relative_path(<span style="color: #800080">$path1</span>,<span style="color: #800080">$path2</span><span style="color: #000000">){
    </span><span style="color: #800080">$arr1</span> = <span style="color: #008080">explode</span>(DIRECTORY_SEPARATOR,<span style="color: #008080">dirname</span>(<span style="color: #800080">$path1</span><span style="color: #000000">));
    </span><span style="color: #800080">$arr2</span> = <span style="color: #008080">explode</span>(DIRECTORY_SEPARATOR,<span style="color: #008080">dirname</span>(<span style="color: #800080">$path2</span><span style="color: #000000">));
    </span><span style="color: #800080">$length</span> = <span style="color: #008080">count</span>(<span style="color: #800080">$arr2</span><span style="color: #000000">);
    </span><span style="color: #0000ff">for</span>(<span style="color: #800080">$i</span> = 0; <span style="color: #800080">$i</span> < <span style="color: #800080">$length</span>; <span style="color: #800080">$i</span>++<span style="color: #000000">){
        </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$arr1</span>[<span style="color: #800080">$i</span>] != <span style="color: #800080">$arr2</span>[<span style="color: #800080">$i</span>]){ <span style="color: #008000">//</span><span style="color: #008000">进行路径匹配</span>
            <span style="color: #0000ff">break</span><span style="color: #000000">;
        }
    }
    </span><span style="color: #0000ff">if</span>(<span style="color: #800080">$i</span> == 0){                    <span style="color: #008000">//</span><span style="color: #008000">根目录不同</span>
        <span style="color: #0000ff">return</span> '不在同一个目录中'<span style="color: #000000">;
    }</span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">$i</span> != 0 && <span style="color: #800080">$i</span> < <span style="color: #800080">$length</span>){        <span style="color: #008000">//</span><span style="color: #008000">在同一个个目录下</span>
        <span style="color: #800080">$separator</span> = <span style="color: #008080">array_fill</span>(0, (<span style="color: #800080">$length</span>-<span style="color: #800080">$i</span>), '..'<span style="color: #000000">);
        </span><span style="color: #0000ff">return</span>(<span style="color: #008080">implode</span>(DIRECTORY_SEPARATOR, <span style="color: #008080">array_merge</span>(<span style="color: #800080">$separator</span>, <span style="color: #008080">array_slice</span>(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$i</span><span style="color: #000000">))));
    }</span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(<span style="color: #800080">$i</span> == <span style="color: #800080">$length</span>){                <span style="color: #008000">//</span><span style="color: #008000">路径完全相同 http://www.manongjc.com/article/1343.html</span>
        <span style="color: #0000ff">return</span> '.'<span style="color: #000000">;
    }
}
</span><span style="color: #800080">$path1</span> = 'F:\Apache\www\temp\php_demo\demo.md'; <span style="color: #008000">//</span><span style="color: #008000">参照路径path1</span>
<span style="color: #800080">$path2</span> = 'F:\Apache\www\temp\php_demo\demo.md'<span style="color: #000000">;
</span><span style="color: #800080">$path3</span> = 'F:\Apache\www\temp\sd\ss.d\sdsfd'<span style="color: #000000">;
</span><span style="color: #800080">$path4</span> = 'G:\FFOutput'<span style="color: #000000">;
</span><span style="color: #008080">var_dump</span>(get_relative_path(<span style="color: #800080">$path1</span>,<span style="color: #800080">$path2</span><span style="color: #000000">));
</span><span style="color: #008080">var_dump</span>(get_relative_path(<span style="color: #800080">$path1</span>,<span style="color: #800080">$path3</span><span style="color: #000000">));
</span><span style="color: #008080">var_dump</span>(get_relative_path(<span style="color: #800080">$path1</span>,<span style="color: #800080">$path4</span><span style="color: #000000">));
</span>?>

 

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