Would be possible to return the result of preg_match under the same array index in case of multiple patterns. What I mean I have the following preg_match what is checking occurrences on different sub strings ('&q' , '?q' , '&keywords').
preg_match('/&q=(.+?)(&|$)|\?q=(.+?)(\&|$)|\&keywords=(.+?)(\&|$)/', urldecode($test_string), $matches);
I would like to see all occurrences under $matches[1] to can exclude the following if statement.
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]);
}
}
Simplify your regex and convert all non-intented capturing groups to non-capturing groups in your regex like this:
Alternatively use parse_str function to parse a query string.