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 ));
?>