samedi 30 juillet 2016

Matching both preg_matches in an if statement

I have 2 preg_match in an if statement, and if either of them are true, I want to print_r both of them. But for some reason, only the first preg_match is being matched each time, even though both of them has the same pattern. Why is this happening?

<?php

$string = "how much is it?";
if (preg_match("~b(how (much|many))b~", $string, $match1) || preg_match("~b(how (much|many))b~", $string, $match2)) {
print_r($match1);
print_r($match2);   
}

?>

Result:

Array ( [0] => how much [1] => how much [2] => much )

Expected Result:

Array ( [0] => how much [1] => how much [2] => much )
Array ( [0] => how much [1] => how much [2] => much )

Aucun commentaire:

Enregistrer un commentaire