10

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 ||

tuxtimo
  • 2,730
  • 20
  • 29
10now
  • 387
  • 2
  • 3
  • 14

9 Answers9

10

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'.

Veger
  • 37,240
  • 11
  • 105
  • 116
  • Would it not work just to write TRUE? Rather than NOT EQUALS? – Gary Carlyle Cook Mar 08 '18 at 05:10
  • 2
    Might be, but the [`strpos()` function](http://php.net/manual/en/function.strpos.php) defines that it returns `false` if needle was not found. So according to the documentation you need to check on 'not false' – Veger Mar 08 '18 at 08:34
5

An alternative: Searches any length of words in the longer string.

Since you've haven't picked an answer from all the strpos answers (most of which should work with just two words, try this function of mine which exceeds word limits. It can find any varying length of words from the longer string (but doesn't use strpos). I think with strpos, you would have to know the number of words to determine how many || you should use or make use of a loop (sort of). This method eliminates all that and gives you a more flexible way to reuse your codes. I thinks codes should be flexible, reusable and dynamic. Test it and see if it does what you want!

function findwords($words, $search) {
    $words_array = explode(" ", trim($words));
    //$word_length = count($words_array);

    $search_array = explode(" ", $search);
    $search_length = count($search_array);

    $mix_array = array_intersect($words_array, $search_array);
    $mix_length = count($mix_array);

    if ($mix_length == $search_length) {
        return true;
    } else {
        return false;
    }
}



 //Usage and Examples

    $words = "This is a long string";
    $search = "is a";

    findwords($words, $search);

    // $search = "is a"; // returns true
    // $search = "is long at"; // returns false
    // $search = "long"; // returns true
    // $search = "longer"; // returns false
    // $search = "is long a"; // returns true
    // $search = "this string"; // returns false - case sensitive
    // $search = "This string"; // returns true - case sensitive
    // $search = "This is a long string"; // returns true
Community
  • 1
  • 1
Steward Godwin Jornsen
  • 1,181
  • 1
  • 7
  • 13
2
$a = 'how are be';
if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
   echo 'contains';
}
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
1

Try:

if (strpos($a,'are') !== false || strpos($a,'be') !== false)
    echo 'what you want';
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
1
if ((strpos($a,'are') !== false) || (strpos($a, 'be') !==false) {
    echo 'contains';
}
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
1

Is this what you want?

if ((strpos($a,'are') !== false) || (strpos($a,'be') !== false)) {
    echo 'contains';
}
RainHeart257
  • 305
  • 1
  • 9
1
if (strpos($a,'are') !== false || strpost($a, 'be') !== false) {
    echo "contains";
}

Brain Candy: If the first one returns true, it'll skip the second check. So both can be true. If the first one is false, ONLY then will it check the second. This is called a short circuit.

Steve's a D
  • 3,801
  • 10
  • 39
  • 60
1
if(strstr($a,'are') || strstr($a,'be')) echo 'contains';

Hum, like this?

Sam
  • 2,950
  • 1
  • 18
  • 26
0
if (strpos($a,'are') || strpos($a, 'be') {
echo 'contains';
}
Kyle Banerjee
  • 2,554
  • 4
  • 22
  • 30
  • Note that if the string starts with `are` or `be`, your approach will fail as `strpos()` will return `0`, which will evaluate to `false`. Use `!== false` as others have proposed. – BenMorel Jun 20 '15 at 11:52