Eric @ EricBess WebHome

胜不骄,败不馁,胸有激雷而面如平湖

Chinese (Simplified) flagItalian flagKorean flagPortuguese flagEnglish flagGerman flagFrench flagSpanish flagJapanese flagArabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flagCroat flagDanish flagFinnish flagHindi flagPolish flagRumanian flagSwedish flagNorwegian flagCatalan flagFilipino flagHebrew flagIndonesian flagLatvian flagLithuanian flagSerbian flagSlovak flagSlovenian flagUkrainian flagVietnamese flag
By N2H

FindRelativePath Function[找出路径间相对关系]

PHP FUNCTION:Found the relative between the path.
PHP函数:找出路径间相对关系。

Example:

1
2
3
4
5
6
7
8
9
<?php
$path_a = 'http://www.php.net/manual/en/install.php';
$path_b = 'http://www.php.net/downloads';
 
echo "\nPath A:  " . $path_a;
echo "\nPath B:  " . $path_b;
echo "\nA to B:  " . findRelativePath($path_a, $path_b); //  ../../downloads/
echo "\nB to A:  " . findRelativePath($path_b, $path_a); //  ../manual/en/install.php
?>

Function:

?Download frpath.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
function findRelativePath($path_1, $path_2)
{
    if ($path_1 == ""  ||  $path_2 == "")
    {
        return false;
    }
 
    preg_match_all("/^(\\/|\w:\\/|https?:\\/\\/[^\\/]+\\/)?(.*)$/i", $path_1, $matches_1, PREG_SET_ORDER);
    preg_match_all("/^(\\/|\w:\\/|https?:\\/\\/[^\\/]+\\/)?(.*)$/i", $path_2, $matches_2, PREG_SET_ORDER);
 
    if ($matches_1[0][1] != $matches_2[0][1])
    {
        return false;
    }
 
    $path_1_parts = explode("/", $matches_1[0][2]);
    $path_2_parts = explode("/", $matches_2[0][2]);
 
    while (isset($path_1_parts[0])  &&  isset($path_2_parts[0]))
    {
        if ($path_1_parts[0] != $path_2_parts[0])
        {
            break;
        }
 
        array_shift($path_1_parts);
        array_shift($path_2_parts);
    }
 
    for ($i = 0, $path = ""; $i < count($path_1_parts)-1; $i++)
    {
        $path .= "../";
    }
    return $path . implode("/", $path_2_parts);
}
?>
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
-欢迎为本文评级

相关日志

本文读者也关心以下内容:

  • N/A

07月 17th, 2008 作者: eric | Coding | Trackback ? | 2 评论| Email This Post Print This Post | 37 views

Add a Comment

Leave a reply

Hide Post Comments
  1. traver posted the following on 2008-09-02 at 12:04 am.

    请问这个代码编辑是什么插件实现的 麻烦告诉下 谢谢

    Reply
    1. eric posted the following on 2008-09-02 at 12:41 am.

      wp-codebox

      Reply