strpos with two words to find

10now picture 10now · Mar 1, 2013 · Viewed 17.4k times · Source

I found this example on stackoverflow:

if (strpos($a,'are') !== false) {
    echo 'true';
}

But how do I make it search for two words. I need something like this: if $a contains the words "are" or "be" or both echo "contains";

I tried xor and ||

Answer

Veger picture Veger · Mar 1, 2013

Just check both words separately and use the boolean or-operator to check if either one or both are contained in $a:

if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
  echo "contains";
}

Note that due to the or-operator, the second check (for 'be') is not performed if the first one already showed that $a contains 'are'.