strpos with two words to find
Asked Answered
L

10

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

Larondalarosa answered 1/3, 2013 at 14:3 Comment(0)
S
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'.

Sumptuous answered 1/3, 2013 at 14:6 Comment(2)
Would it not work just to write TRUE? Rather than NOT EQUALS?Undetermined
Might be, but the strpos() function defines that it returns false if needle was not found. So according to the documentation you need to check on 'not false'Sumptuous
L
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
Latta answered 1/3, 2013 at 14:38 Comment(0)
N
2
$a = 'how are be';
if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
   echo 'contains';
}
Nmr answered 1/3, 2013 at 14:8 Comment(0)
B
1

Try:

if (strpos($a,'are') !== false || strpos($a,'be') !== false)
    echo 'what you want';
Brusque answered 1/3, 2013 at 14:6 Comment(0)
C
1
if ((strpos($a,'are') !== false) || (strpos($a, 'be') !==false) {
    echo 'contains';
}
Comport answered 1/3, 2013 at 14:6 Comment(0)
A
1

Is this what you want?

if ((strpos($a,'are') !== false) || (strpos($a,'be') !== false)) {
    echo 'contains';
}
Ammo answered 1/3, 2013 at 14:6 Comment(0)
A
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.

Artel answered 1/3, 2013 at 14:7 Comment(0)
S
1
if(strstr($a,'are') || strstr($a,'be')) echo 'contains';

Hum, like this?

Subinfeudate answered 1/3, 2013 at 14:8 Comment(0)
S
0
if (strpos($a,'are') || strpos($a, 'be') {
echo 'contains';
}
Seumas answered 1/3, 2013 at 14:10 Comment(1)
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.Contrastive
L
0

I'd do something like this:

$string_vals = "jar,jar,binks"; 
$string_arr_jjb = explode(",", $string_vals);

foreach($string_arr as $string_cur) {
      if(strpos(whatever_usa_tryna_match-againsta, $string_cur) !== false)
}
Laszlo answered 13/6 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.