[英]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.