Use PHP to calculate the relative paths of two paths

Source: Internet
Author: User
This article describes how to use PHP to calculate the relative paths of two paths. For more information, see The code is as follows:




Function relativePath ($ aPath, $ bPath ){
$ Shard R = explode ('/', $ aPath); // The explode function is used to split a string and return the split array. here, the string is split '/'.
$ BArr = explode ('/', $ bPath );
$ ADiffToB = array_diff_assoc ($ region R, $ bArr); // array_diff_assoc () is used to obtain the difference set of elements between array A and array B. different keys and values are considered different elements, elements in array A and different from array B are returned here.
$ Count = count ($ aDiffToB );

$ Path = '';
For ($ I = 0; $ I <$ count-1; $ I ++ ){
$ Path. = '../';
}

$ Path. = implode ('/', $ aDiffToB); // implode () is used to connect an array element with a specified string. here, the string after the array element is connected with '/' is returned.

Return $ path;
}

Echo relativePath ('/a/B/c/d/a. php','/a/B/1/2/B. php ');
?>



Page output
../C/d/a. php

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.