PHP function compute relative paths of two files

Source: Internet
Author: User
Sina has an interview question: write a php function to calculate the relative path of the two files? Php // calculate c. php Vs. e. the relative path of php should be .. /.. /12/34 $ a =/a/B/c/d/e. php; $ B =/a/B/12/34/c. php; getpathinfo ($ a, $ B); fun... "> <LINKhref =

 

Sina has an interview question: write a php function to calculate the relative path of the two files? Php // calculate c. php Vs. e. the relative path of php should be .. /.. /12/34 $ a =/a/B/c/d/e. php; $ B =/a/B/12/34/c. php; getpathinfo ($ a, $ B); function getpathinfo ($ a, $ B) {$

Sina has an interview question: write a php function to calculate the relative path of the two files

  

// Calculate the relative path of c. php to e. php. it should be ../12/34

$ A = '/a/B/c/d/e. php ';

$ B = '/a/B/12/34/c. php ';

Getpathinfo ($ a, $ B );

Function getpathinfo ($ a, $ B ){

$ A2array = explode ('/', $ );

$ B2array = explode ('/', $ B );

$ Pathinfo = '';

For ($ I = 1; $ I <= count ($ b2array)-2; $ I ++ ){

$ Pathinfo. = $ a2array [$ I] = $ b2array [$ I]? '../': $ B2array [$ I]. '/';

}

Print_R ($ pathinfo );

}

?>

There is also a complicated method that is not perfect. The idea is correct and can be studied.

  

$ A = 'AA/bb/cc/dd/a. php ';

$ B = 'AA/bb/11/22/33/B. php ';

// Write a function that counts the relative paths of two files.

Function GetNum ($ variant, $ variant2 ){

$ Pth1 = null;

$ Pth1 = null;

$ Tmp = array ();

// Determine whether the files under the path exist.

If (is_file ($ variant) & is_file ($ variant2 )){

$ Len1 = count ($ pth1 = explode ('/', dirname ($ variant )));

$ Len2 = count ($ PH2 = explode ('/', dirname ($ variant2 )));

$ Maxlen = max ($ len1, $ len2 );

For ($ I = 1; $ I <$ maxlen; $ I ++ ){

If ($ pth1 [$ I]! = $ P2[ $ I] & isset ($ pth1 [$ I]) {

If (isset ($ p2[ $ I]) $ tmp [] = $ p2[ $ I];

} Else {

$ Tmp [] = $ p2[ $ I];

$ Pathe. = '../';

}

}

Return $ pathe. implode ('/', $ tmp). '/'. basename ($ variant2 );

} Else {

The return 'path is invalid! ';

}

}

Print_r (GetNum ($ a, $ B ));

?>

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.