Home > Backend Development > PHP Tutorial > Find the relative paths of two directories

Find the relative paths of two directories

WBOY
Release: 2016-07-25 08:48:34
Original
981 people have browsed it
Find the relative paths of two directories without limiting the path depth
  1. /**
  2. * Output the relative path of $b relative to $a ($a)
  3. * No limit on path depth, no optimization, just implementation of the function
  4. */
  5. function getPath($a, $b)
  6. {
  7. $aArr = explode('/', dirname($a));
  8. $bArr = explode ('/', dirname($b));
  9. $aLen = count($aArr);
  10. $bLen = count($bArr);
  11. $len = max($aLen, $bLen );
  12. $k = 0;
  13. for($i = 0; $i < $len; $i++)
  14. {
  15. if($k == 0)
  16. {
  17. if(isset($aArr[$i]) && ($aArr[$i] != $bArr[$i]))
  18. {
  19. $d .= '../';
  20. if( isset($bArr[$i]))
  21. {
  22. $nP[$i] = $bArr[$i];
  23. }
  24. $k = $k + 1;
  25. }
  26. }
  27. else
  28. {
  29. if(isset($aArr[$i]))
  30. {
  31. $d .= '../';
  32. }
  33. if(isset($bArr[$i] ))
  34. {
  35. $nP[$i] = $bArr[$i];
  36. }
  37. }
  38. }
  39. echo $d.implode('/', $nP);
  40. }
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template