How to use preg_match with multiple words in PHP?
13
I want to search for multiple words in a string using PHP's preg_match()
function.
Here's what I'm trying:
$string = "white red green blue";
$pattern = "/red, blue/";
echo preg_match($pattern, $string);
This returns 0, but I expected it to find matches. How do I properly search for multiple words using regular expressions in PHP?
1 Answer
0
You can use multiple words in pattern 2 or more for preg_match, like this:
$string = "white red green blue";
$pattern = "/(red)|(blue)/";
echo preg_match($pattern, $string);
result : 1