gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:58:23 25 4
gpt4 key购买 nike

您好,我必须在一个字符串中搜索两个字符串。例如

$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 循环。但我希望它仅在两个字符串都存在于父字符串中时才进入 if 条件。我怎样才能做到这一点。

注意:我不想遍历字符串。 (就像 explode 字符串和 foreach 展开后的数组并使用 strpos 搜索)

最佳答案

试试看

if(preg_match("/(brown)/i", $string) && preg_match("/(lazy)/i", $string)){
execute my code
}

Yon 也可以像 strpos 一样尝试

if(strpos($string, 'brown') >= 0 && strpos($string, 'lazy') >= 0){
execute my code
}

关于php - 如何在一个字符串中搜索两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26645605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com