To seek $b relative to the $a relative path of the PHP code _php tips

Source: Internet
Author: User
Tags explode

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

The answer to the PHP interview question:

Copy 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 an array starts at 0, 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));
}

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.