I'd like to find the common pattern between 2 URLs in PHP. I've played around with https://gist.github.com/chrisbloom7/1021218 but that stops at the point of finding the longest match not accounting for wildcards that exist in URLs.
e.g. here are 2 URLs
If I run the function over these, my common pattern is
http://example.com/collections/
What I'm looking for is
http://example.com/collections/*/products/
Does anyone know how I can adapt the code to make it work or is there a better way?
Instead of regex, split the urls on /
then compare each elements of array then recompose the url:
$url1 = 'http://example.com/collections/dresses/products/foo/dress.html';
$url2 = 'http://example.com/collections/shoes/products/shoe.html';
$part1 = explode('/', $url1);
$part2 = explode('/', $url2);
$common = array();
$len = count($part1);
if (count($part2) < $len) $len = count($part2);
for ($i = 0; $i < $len-1; $i++) {
if ($part1[$i] == $part2[$i]) {
$common[] = $part1[$i];
} else {
$common[] = '*';
}
}
$out = implode('/', $common);
echo "$out\n";
Output:
http://example.com/collections/*/products