10now
10now

Reputation: 397

strpos with two words to find

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

Upvotes: 10

Views: 23137

Answers (10)

Anthony Woodworth
Anthony Woodworth

Reputation: 16

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

Upvotes: 0

Veger
Veger

Reputation: 37905

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

Upvotes: 10

Steward Godwin Jornsen
Steward Godwin Jornsen

Reputation: 1179

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

Upvotes: 5

Kyle Banerjee
Kyle Banerjee

Reputation: 2824

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

Upvotes: 0

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

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

Upvotes: 2

Sam
Sam

Reputation: 2970

if(strstr($a,'are') || strstr($a,'be')) echo 'contains';

Hum, like this?

Upvotes: 1

Steve's a D
Steve's a D

Reputation: 3821

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.

Upvotes: 1

RainHeart257
RainHeart257

Reputation: 305

Is this what you want?

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

Upvotes: 1

Pedro del Sol
Pedro del Sol

Reputation: 2841

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

Upvotes: 1

xlecoustillier
xlecoustillier

Reputation: 16361

Try:

if (strpos($a,'are') !== false || strpos($a,'be') !== false)
    echo 'what you want';

Upvotes: 1

Related Questions