如何在一个字符串中搜索两个字符串?

Mad*_*gle 2 php string

嗨,我必须在一个字符串中搜索两个字符串.例如

$string = "The quick brown fox jumped over a lazy cat";
if($string contains both brown and lazy){
  then execute my code
}
Run Code Online (Sandbox Code Playgroud)

我试过像这样的pregmatch,

if(preg_match("/(brown|lazy)/i", $string)){
    execute my code
}
Run Code Online (Sandbox Code Playgroud)

但如果字符串中存在其中一个,则输入if循环.但我希望它只有在父字符串中存在两个字符串时才进入if条件.我怎样才能做到这一点.

注意:我不想循环遍历字符串.(就像explode字符串和foreach爆炸数组一样,并使用搜索strpos)

Gau*_*164 5

试试吧

if(preg_match("/(brown)/i", $string) && preg_match("/(lazy)/i", $string)){
    execute my code
}
Run Code Online (Sandbox Code Playgroud)

延亨默也可以尝试strpos

if(strpos($string, 'brown') >= 0 && strpos($string, 'lazy') >= 0){
    execute my code
}
Run Code Online (Sandbox Code Playgroud)