在多個模式的情況下,可能會返回preg_match在相同數組索引下的結果。我的意思是我有以下preg_match檢查不同子字符串('&q'
,'?q'
,'&keywords'
)的出現。php preg_match()多種模式
preg_match('/&q=(.+?)(&|$)|\?q=(.+?)(\&|$)|\&keywords=(.+?)(\&|$)/', urldecode($test_string), $matches);
我想看到$matches[1]
下所有出現的地方可以排除以下if
聲明。
if($matches){
if ($matches[1] != ''){
$query_p = mysql_escape_string($matches[1]);
} elseif ($matches[3] != ''){
$query_p = mysql_escape_string($matches[3]);
} elseif($matches[5] != ''){
$query_p = mysql_escape_string($matches[5]);
}
}
這看起來像您試圖從url的查詢部分提取元素。有這方面的專門手段,你爲什麼要使用正則表達式? – arkascha
是的,這將是想法,從url獲取查詢部分。你能指點我到正確的區域嗎? – fefe
查看phps'parse_url()'函數並用'&'字符爆炸查詢結果部分:http://php.net/manual/en/function.parse-url.php – arkascha