PHP code together to find the PHP code for $b relative path relative to $ A

Source: Internet
Author: User
The topic of the PHP interview question:
$a = '/a/b/c/d/e.php '; $b = '/a/b/12/34/c.php '; Calculate $b relative path relative to $a should be: /.. /c/d
Answers to the PHP interview questions:

Copy the Code code as follows:


function getrelative ($a, $b) {
$arr = Explode ("/", $a);
$BRR = Explode ("/", $b);
$c = count ($arr)-2;
$d = count ($BRR)-2;
Minus two, one is not in the back of the file name,
One is that the index of the array starts at 0, which is smaller than the number of the first dimension in the array.
$e = ($c > $d)? $c: $d;
$str 1 = $str 2 = ";
for ($j =0; $j <= $e; $j + +) {
$cur _a = isset ($arr [$j])? $arr [$j]: ';
$cur _b = isset ($brr [$j])? $BRR [$j]: ';
if ($cur _a = = $cur _b) {
Continue
} else {
if ($j <= $c)
{
$str 1.= '/'. $cur _a;
}
if ($j <= $d)
{
$str 2.= ". /";
}
}
}
Return $str 2.substr ($str 1,1,strlen ($str 1));
}

The above describes the PHP code together to find $b relative to $ A relative path of the PHP code, including the contents of the PHP code, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.