如何检查字符串是否包含两个或更多个特定单词

时间:2018-10-06 13:17:01

标签: php string

我需要检查字符串是否包含两个或更多个特定的单词,如果找到了,则回显单词。.我尝试使用此..但是,如果找到一个单词,则找到相应的单词,并且现在可以工作了:

尝试代码:

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

所以我尝试使用以下示例:

$string = 'Today is wonderfull day w63';

上面的功能什么都不做。

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

此字符串需要从上面的函数echo 'found'回显。

那么,如何在PHP中检查字符串是否包含所有三个特定的单词,以及是否包含找到的所有三个回显?

1 个答案:

答案 0 :(得分:6)

这是一种可能的解决方案

if(preg_match("/(?=.* s )(?=.*g898)(?=.*w63)/i", $string)){
    echo 'found';
}