How to preg_match multiple words in PHP?

#21
272
1 Answer
Question Image

I have multiple words, I want to preg_match these words in my input string, such as:

$string = "white red green blue";

$pattern = "/red, blue/";

echo preg_match($pattern, $string);

my result is 0, Please tell me how it possible.


Related to:
preg_matchpatternstringphp
0
Answer
Answer 1 of 1
# 14

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

0
0
0
Related Articles

If you still have a question about this, submit it in our Q&A community - Ask Question