Skip to content

Instantly share code, notes, and snippets.

@slopjong
Created May 10, 2014 18:49
Show Gist options
  • Save slopjong/3c0a1bc1d545a33ff5f8 to your computer and use it in GitHub Desktop.
Save slopjong/3c0a1bc1d545a33ff5f8 to your computer and use it in GitHub Desktop.
<?php
$paths = array(
"/new/space/afsdfadsfthe_space_name/status/json",
"/vagrant/public/space/afsdfadsfthe_space_name/index.php"
);
echo get_common_path($paths[0], $paths[1]);
// this is not yet
function get_common_path($path1, $path2) {
// in the future the function should accept an array of paths,
// as of now if more than two paths are provided, this will fail
// because any common path elements which exist in the other arrays
// are considered 'common' but only those elements that next to the
// other without hole in the array are the real common elements.
$paths = array($path1, $path2);
$exploded_paths = array();
foreach ($paths as $path) {
$exploded_paths[] = explode('/', trim($path, '/'));
}
// initialize the common paths array
$common_path = $exploded_paths[0];
foreach ($exploded_paths as $path) {
$common_path = array_intersect($path, $common_path);
}
return '/' . join('/', $common_path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment