Heim  >  Artikel  >  php教程  >  Der PHP-Quellcode berechnet die relativen Pfade zweier Dateien

Der PHP-Quellcode berechnet die relativen Pfade zweier Dateien

WBOY
WBOYOriginal
2016-10-28 15:03:261374Durchsuche
<?<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>?>

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn