嗨我必須在一個字符串中搜索兩個字符串。 例如如何在一個字符串中搜索兩個字符串?
$string = "The quick brown fox jumped over a lazy cat";
if($string contains both brown and lazy){
then execute my code
}
我試圖pregmatch這樣,
if(preg_match("/(brown|lazy)/i", $string)){
execute my code
}
但是它進入如果如果它們中的一個存在於串中循環。但是我希望它只在兩個字符串出現在父字符串中時才輸入if條件。我怎樣才能做到這一點。
注意:我不想在字符串上循環。 (就像使用strpos
explode
字符串和foreach
分解的陣列上和搜索)
'$ string contains the brown and lazy'這裏的關鍵字是:'和'。 – Shomz 2014-10-30 05:21:47
'$ regex =「/(brown)[^。] *(懶惰)/ i」;'要短得多。 – 2014-10-30 05:24:41
如果你正在尋找一種方法來處理正則表達式,這篇文章解釋瞭如何用正則表達式獲得'和'效果:http://stackoverflow.com/questions/469913/regular-expressions-is-there -an-and-operator – Tim 2014-10-30 05:24:44