I went to company X for an interview a few days ago. I finally got defeated due to salary issues! Pull the exam questions out and study them! Question: write a function to calculate the relative path of the two files, for example, $ a = '/a/B/c/d/e. php '; $ B ='/a/B/12/34/c. php '; calculates the relative path of $ B relative to $ .. /.. /c/d answer :? PhpfunctiongetR
I went to company X for an interview a few days ago. I finally got defeated due to salary issues!
Pull the exam questions out and study them!
Question: write a function to calculate the relative path of the two files, for example, $ a = '/a/B/c/d/e. php '; $ B ='/a/B/12/34/c. php '; calculates the relative path of $ B relative to $ .. /.. /c/d
A:
Function getRelative ($ a, $ B)
{
$ Arr_a = explode ("/", $ );
$ Brr_ B = explode ("/", $ B );
$ I = 1;
While (true ){
If ($ arr_a [$ I] = $ brr_ B [$ I]) {
$ I ++;
} Else {
Break;
}
}
$ C = count ($ brr_ B );
$ D = count ($ arr_a );
$ E = ($ c> $ d )? $ C: $ d;
$ Str1 = '';
$ Str2 = '';
For ($ j = $ I; $ j <$ e; $ j ++ ){
If (isset ($ arr_a [$ j]) {
If ($ j <($ D-1 )){
$ Str1. = $ arr_a [$ j]. "/";
} Else {
$ Str1. = $ arr_a [$ j];
}
}
If (isset ($ brr_ B [$ j]) {
$ Str2. = "../";
}
}
Return $ str2. $ str1;
}
$ A = "/c/B/c/d/k/h/t/e. php ";
$ B = "/a/B/e/f/h. php ";
$ Relative = getRelative ($ a, $ B );
Var_dump ($ relative );
?>
Running result: string (34 )".. /.. /.. /.. /.. /c/B/c/d/k/h/t/e. php"
End up!