First the original question is this:
Write a function that calculates the relative path of two files, such as $a = '/a/b/c/d/d.php ', $b = '/a/b/1/2/c.php '
The relative path of the calculated $b relative to $ A should be:. /.. /c/d
So for the answer on the Internet, look at the seemingly only for the problem given in the path to calculate, if the path changes, similar to the following, then those programs seem useless, of course, some are useful, but still report some warnings come out ...
For example:
$a = '/a/b/c/d/e/f/g/h/e.php '
$b = '/a/b/1/2/c.php '
Again for example:
$a = '/a/e.php '
$b = '/a/b/c/d/1/2/c.php '
Again for example:
$a = '/a/b/c/d/d.php '
$b = '/a/b/c/d/c.php '
So this is a lot of things, can not just look at the needs of the present!
First of all, it is the intention of the topic, it is to ask $b relative to a $ A relative path, that is, what is the meaning of it.
Is that the file given by $b is represented by a relative path, how can I find the file that corresponds to $ A. To explain the path that the topic gives, is how we $b to $ A, which starts from $b. /"top-level directory and then". /"The previous directory came to the"/a/b "directory, and then started from this directory, connected to"/c/d"and then came to the corresponding directory of $ A.
In other words, the main task of this topic is to find the " top levels" from $b to $ A to get to a place where $ A has the same directory, then connect to the next part of $ A, then we get the answer, so the key to the topic is to find the exact "upper levels"!
Don't say much nonsense, look directly at the code:
/** * Calculates the relative path of $b relative to $ A * @param string $a * @param string $b * @return string */function getrelativepath ($a, $b) {$relativeP Ath = ""; $pathA = explode ('/', dirname ($a)), $pathB = explode ('/', dirname ($b)); $n = 0; $len = count ($pathB) > count ($pat HA)? Count ($pathA): Count ($pathB);d o {if ($pathA [$n]! = $pathB [$n] | | $n >= $len) {break,}} while (+ + $n); $relativePath. = Str_repeat ('.. /', COUNT ($pathB)-$n); $relativePath. = Implode ('/', Array_splice ($pathA, $n)); return $relativePath;} $res = Getrelativepath ($a, $b); Var_dump ($res);
After testing, the above list of conditions are satisfied.
What this procedure needs to explain is:
Why do $len need to ask for the least number of paths in $ A and $b?
That's because we're going through do{}while (); Loop to start from 0 that is starting the path down has been compared to the path of $ A and $b, hoping to find out which path started to cause a $ A and $b different, that is, from the find a $ A and $b path total the same number of paths is several. That in the process of looking for, if a $ A and $b path has been unequal then $n will always increase lead into a dead loop, so we need another condition, that is, we need to let $n maximum value can not exceed $ A and the shortest path in $b, why? Because once the $n is larger than $len that means that there is a path that has ended, it does not find the same part of $ A and $b until the end, which means that the shortest path length is exactly the same as the number of their paths. (Most other programs have come up with this error), and there's no need to keep looking because we've found it.
After finding the number of the same path, the first thing to calculate is: from $b to $ A, you need a few ". /", then the calculation method is to use" count ($pathB)-$n ", why? Because $n represents the same number of paths, and "count ($pathB)" represents the number of paths to $b, each of them gets a different number of paths, which is required. /", then the result after the reduction must be greater than the value of 0, so no problem!
Find out from $b to $ A requires a few ". /"After the task is to put these several". /"and a different path part of $ A can be stitched together, i.e. code: $relativePath. = Implode ('/', Array_splice ($pathA, $n));
Here it is, the end!
A PHP interview problem, find the relative path of two files