matching url to another url
I am faced with rather unusual situation
I will have url in any of the 3 formats:
- http://example.com/?p=12
- http://example.com/a-b/
- http://example.com/a.html 开发者_StackOverflow社区
Now, I need to match with a url like
- http://example.com/?p=12&t=1
- http://example.com/a-b/?t=1
- http://example.com/a.html?t=1
How can I achieve this? Please help
I know I can use like:
stristr('http://example.com/?p=12','http://example.com/?p=12&t=1')
but this will also match when
http://example.com/?p=123 (as it matches p=12)
Help guys, please.
A simple way to accomplish this would be to use PHP's parse_url()
and parse_str()
.
http://www.php.net/manual/en/function.parse-url.php
http://www.php.net/manual/en/function.parse-str.php
Take your urls and run them through parse_url()
, and take the resulting $result['query']
. Run these through parse_str()
and you'll end up with two associative arrays of the variable names and their values.
Basically, you'll want to return true if the $result['path']
s match, and if any keys which are in both $result['query']
contain the same values.
code example:
function urlMatch($url1, $url2)
{
// parse the urls
$r1 = parse_url($url1);
$r2 = parse_url($url2);
// get the variables out of the queries
parse_str($r1['query'], $v1);
parse_str($r2['query'], $v2);
// match the domains and paths
if ($r1['host'] != $r2['host'] || $r1['path'] != $r2['path'])
return false;
// match the arrays
foreach ($v1 as $key => $value)
if (array_key_exists($key, $v2) && $value != $v2[$key])
return false;
// if we haven't returned already, then the queries match
return true;
}
A very quick (and somewhat dirty) way to achieve this is via the following regex:
$regex = '#^' . preg_quote($url, '#') . '[?&$]#';
Where $url
is the URL you need to search for. In the above, we look for the URL in the beginning of whatever the regex is matched upon, followed by either a ?
, a &
or the end-of-line anchor. This is not bullet-proof but may be sufficient (@Mala already posted the "right" approach).
Below, I've posted an example of use (and the result):
$urls = array(
'http://example.com/?p=12',
'http://example.com/a-b/',
'http://example.com/a.html'
);
$tests = array(
'http://example.com/?p=12&t=1',
'http://example.com/a-b/?t=1',
'http://example.com/a.html?t=1',
'http://example.com/?p=123'
);
foreach ($urls as $url) {
$regex = '#^' . preg_quote($url, '#') . '[?&$]#';
print $url . ' - ' . $regex . "\n";
foreach ($tests as $test) {
$match = preg_match($regex, $test);
print ' ' . ($match ? '+' : '-') . ' ' . $test . "\n";
}
}
Result:
http://example.com/?p=12 - #^http\://example\.com/\?p\=12[?&$]#
+ http://example.com/?p=12&t=1
- http://example.com/a-b/?t=1
- http://example.com/a.html?t=1
- http://example.com/?p=123
http://example.com/a-b/ - #^http\://example\.com/a-b/[?&$]#
- http://example.com/?p=12&t=1
+ http://example.com/a-b/?t=1
- http://example.com/a.html?t=1
- http://example.com/?p=123
http://example.com/a.html - #^http\://example\.com/a\.html[?&$]#
- http://example.com/?p=12&t=1
- http://example.com/a-b/?t=1
+ http://example.com/a.html?t=1
- http://example.com/?p=123
精彩评论