1

I need to check if string contains two or more specific words if so echo words found..i try using this..but if one word is found then it echo found and it now works:

Try code:

if(preg_match("/(s|g898|w63)/i", $string)){
    echo 'found';
}

So i try using this example:

$string = 'Today is wonderfull day w63';

Function above need to do nothing.

$string = 'Above the sky limit s g898 w63';

This string need to echo from above function echo 'found'.

So how to check in PHP if string containts all three specific word and if contains all three echo found?

3
  • Have you tried to put the words you are looking for in an array and loop thru this array?
    – Muiter
    Commented Oct 6, 2018 at 13:20
  • 1
    What if the same word is found twice? Does that count as two words? php.net/manual/en/function.preg-replace.php and the fifth parameter come to mind Commented Oct 6, 2018 at 13:21
  • strpos first word, strpos second word.
    – u_mulder
    Commented Oct 6, 2018 at 13:22

1 Answer 1

6

that is one possible solution

if(preg_match("/(?=.* s )(?=.*g898)(?=.*w63)/i", $string)){
    echo 'found';
}
1
  • Word boundaries would improve this. Commented Oct 6, 2018 at 13:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.