与查询字符串匹配的网址
问题描述:
我需要一段基本上可以完成URL匹配任务的代码。我在下面列出了两种情况,以清楚我需要的东西。与查询字符串匹配的网址
匹配URL可能/不可以包含子目录,所以有两种情况。
http://example.com/?pgid=1
http://example.com/?pgid=1&opt2=2
http://example.com/?opt2=2&pgid=1
http://example.com/?pgid=1&opt2=2&opt3=3
http://example.com/?opt2=2&pgid=1&opt3=3
http://example.com/?opt2=2&opt3=3&pgid=1
should match => http://example.com/?pgid=1
和
http://example.com/sub-dir/?pgid=1
http://example.com/sub-dir/?pgid=1&opt2=2
http://example.com/sub-dir/?opt2=2&pgid=1
http://example.com/sub-dir/?pgid=1&opt2=2&opt3=3
http://example.com/sub-dir/?opt2=2&pgid=1&opt3=3
http://example.com/sub-dir/?opt2=2&opt3=3&pgid=1
should match => http://example.com/sub-dir/?pgid=1
我怎样才能达到上述的事情,帮助表示赞赏。谢谢。
答
正则表达式马赫该网址是:\?pgid=1$
据我所知你不想在你的网址中的其他查询字符串参数。
答
我想你的意思是这样:
$input = array('http://example.com/?pgid=1',
'http://example.com/?pgid=1&opt2=2',
'http://example.com/?opt2=2&opt3=3&pgid=1',
'http://example.com/sub-dir/?opt2=2&opt3=3&pgid=1');
foreach($input as $i){
$url = parse_url($i);
echo $url['scheme'].'://'.$url['host'].$url['path'];
parse_str($url['query'],$query);
echo 'pgid='.$query['pgid'];
echo '<br/>';
}
很显然,你应该调整这对您的个人需要,但是这显示了如何提取pgid
与只是参数重建的URL。
然后,您可以进行匹配,可能只需使用$query['pgid']
或使用全新的url。
答
$url = parse_url('http://example.com/?pgid=1&opt2=2&opt3=3');
$against = parse_url('http://example.com/?pgid=1');
$folder = $url['path'];
$query = parse_str($url['query']);
$pgidout = $pgid;
$query = parse_str($against['query']);
if(($url['path'] == $against['path']) && ($pgid == $pgidout)){
echo "matched";
} else {
echo "not matched";
}
惯于首先懒得告诉我们你做了什么,以满足您的需求? – Bhaskar
这将匹配所有网址:'。*'(即,我同意@Bhaskar) – deceze