[英]matching urls with query string
我需要一段基本上完成匹配URL任务的代码。 我在下面列出了2种情况,以明确我的需求。
匹配URL可能/可能不包含子目录,因此有2种情况。
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$
据我了解,您不需要您的url中的任何其他查询字符串参数。
我想你的意思是这样的:
$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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.