Created
March 22, 2016 14:12
-
-
Save lovelock/0714c065c543b50297a0 to your computer and use it in GitHub Desktop.
PHP version of relative_path_calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$path1 = 'a/b/c/d/e.png'; | |
$path2 = 'a/b/f/g.png'; | |
function pathfinder($path1, $path2) | |
{ | |
$exploded_path1 = explode(DIRECTORY_SEPARATOR, $path1); | |
$exploded_path2 = explode(DIRECTORY_SEPARATOR, $path2); | |
$intersect_array_count = count(array_intersect_assoc($exploded_path1, $exploded_path2)); | |
$depth = count($exploded_path1) - $intersect_array_count; | |
return str_repeat('../', $depth) . implode(DIRECTORY_SEPARATOR, array_slice($exploded_path2, $intersect_array_count - 1)); | |
} | |
echo pathfinder($path1, $path2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment