php计算两个文件相对路径的方法
本文实例讲述了php计算两个文件相对路径的方法。分享给大家供大家参考。具体如下:
一、问题:
写一个php函数算出两个文件的相对路径。例如$a="/a/b/c/d/e.php"; $b="/a/b/12/34/c.php",B相对于A的相对路径是什么?
二、解决方法:
<?php /** * 求$b相对于$a的相对路径 * @param string $a * @param string $b * @return string */ function getRelativePath ($a, $b) { $patha = explode('/', $a); $pathb = explode('/', $b); $counta = count($patha) - 1; $countb = count($pathb) - 1; $path = "../"; if ($countb > $counta) { while ($countb > $counta) { $path .= "../"; $countb --; } } // 寻找第一个公共结点 for ($i = $countb - 1; $i >= 0;) { if ($patha[$i] != $pathb[$i]) { $path .= "../"; $i --; } else { //判断是否为真正的第一个公共结点,防止出现子目录重名情况 for ($j = $i - 1, $flag = 1; $j >= 0; $j --) { if ($patha[$j] == $pathb[$j]) { continue; } else { $flag = 0; break; } } if ($flag) break; else $i ++; } } for ($i += 1; $i <p>希望本文所述对大家的php程序设计有所帮助。</p>
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

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel
R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle -Lösung
2 Wochen vorByDDD
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
1 Monate vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Herunterladen der Mac-Version des Atom-Editors
Der beliebteste Open-Source-Editor

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

ZendStudio 13.5.1 Mac
Leistungsstarke integrierte PHP-Entwicklungsumgebung

VSCode Windows 64-Bit-Download
Ein kostenloser und leistungsstarker IDE-Editor von Microsoft

WebStorm-Mac-Version
Nützliche JavaScript-Entwicklungstools