- <?php
- $a = '/a/b/12/34/e.php';
- $b = '/a/b/12/44/c.php';
- function getRelativePath($a, $b) {
- $returnPath = array(dirname($b));
- $arrA = explode('/', $a);
- $arrB = explode('/', $returnPath[0]);
- for ($n = 1, $len = count($arrB); $n < $len; $n++) {
- if ($arrA[$n] != $arrB[$n]) {
- break;
- }
- }
- if ($len - $n > 0) {
- $returnPath = array_merge($returnPath, array_fill(1, $len - $n, '..'));
- }
- $returnPath = array_merge($returnPath, array_slice($arrA, $n));
- return implode('/', $returnPath);
- }
- echo getRelativePath($a, $b);
时间: 2024-10-29 21:28:15