PHP检查字符串中是否至少包含两个特定单词

PHP检查字符串中是否至少包含两个特定单词,php,preg-match,Php,Preg Match,我正在使用preg\u match检查字符串中是否有特定的单词: $string = 'This string contains a few words that are in an array!'; $arr = ['string', 'few', 'words']; if(preg_match('['.implode('|', $arr).']', $string)){ //do something if there is at least one word from the ar

我正在使用
preg\u match
检查字符串中是否有特定的单词:

$string = 'This string contains a few words that are in an array!';
$arr = ['string', 'few', 'words'];

if(preg_match('['.implode('|', $arr).']', $string)){
    //do something if there is at least one word from the array inside that string
}
这是相当不错的工作,但它将返回真,如果有至少一个字,我需要它返回真,如果有至少两个字从数组中的字符串

这可以一步完成吗?如果没有,我应该从这里走哪条路来获得这个结果


谢谢D

您可以使用preg\u match\u all进行此操作

$string = 'This string contains a few words that are in an array!';
$arr = ['string', 'few', 'words'];
$count = preg_match_all('['.implode('|', $arr).']', $string); // Returns 3

您可以使用preg_match_all进行此操作

$string = 'This string contains a few words that are in an array!';
$arr = ['string', 'few', 'words'];
$count = preg_match_all('['.implode('|', $arr).']', $string); // Returns 3

如果您不关心精确匹配,则选择的答案是正确的。如果要匹配精确的单词,则需要使用单词边界
\b

以下是一个例子:

$arr = ['string', 'few', 'words'];

preg_match_all('#\b('.implode('|', $arr).')\b#', $string, $wordsFound);

$wordsFound = array_unique($wordsFound[0]);

if(count($wordsFound) >= 2){
    echo "Matched";
}else{
    echo "Not matched";
}


Input: $string = 'This string contains a few words that are in an array!';
Output: Matched (3 founds)

Input: $string = 'This string222 contains a few words that are in an array!';
Output: Matched (2 founds)

Input: $string = 'This string222 contains a few23 words that are in an array!';
Output: Not Matched (1 found)

Input: $string = 'This string contains a string words that are in an array!';
Output: Matched (2 founds)

Input: $string = 'This string contains a string string that are in an array!';
Output: Not Matched (1 found)

如果您不关心精确匹配,则选择的答案是正确的。如果要匹配精确的单词,则需要使用单词边界
\b

以下是一个例子:

$arr = ['string', 'few', 'words'];

preg_match_all('#\b('.implode('|', $arr).')\b#', $string, $wordsFound);

$wordsFound = array_unique($wordsFound[0]);

if(count($wordsFound) >= 2){
    echo "Matched";
}else{
    echo "Not matched";
}


Input: $string = 'This string contains a few words that are in an array!';
Output: Matched (3 founds)

Input: $string = 'This string222 contains a few words that are in an array!';
Output: Matched (2 founds)

Input: $string = 'This string222 contains a few23 words that are in an array!';
Output: Not Matched (1 found)

Input: $string = 'This string contains a string words that are in an array!';
Output: Matched (2 founds)

Input: $string = 'This string contains a string string that are in an array!';
Output: Not Matched (1 found)

如果您的要求是知道字符串中至少存在2个所需单词,那么您需要小心。如果字符串中有两个相同的单词,如果只使用
preg\u match\u all
搜索发生的情况,很容易得到误报

这将报告3,即所有3个单词都出现在草堆中

$string = 'This string contains a string and a few other words!';
$finds = ['string', 'few', 'words'];
$findCount = 0;

foreach ($finds as $find) {
    if ( strpos($string, $find) !== false ) $findCount++;
}
echo $findCount;
如果使用此字符串,它将报告2

$string = 'This string contains a string and a other words!';
最重要的是,如果您使用包含单词
string
的字符串两次,而不是所需单词中的两个,则只报告1

$string = 'This string contains a string and other stuff!';

如果您的要求是知道字符串中至少存在2个所需单词,那么您需要小心。如果字符串中有两个相同的单词,如果只使用
preg\u match\u all
搜索发生的情况,很容易得到误报

这将报告3,即所有3个单词都出现在草堆中

$string = 'This string contains a string and a few other words!';
$finds = ['string', 'few', 'words'];
$findCount = 0;

foreach ($finds as $find) {
    if ( strpos($string, $find) !== false ) $findCount++;
}
echo $findCount;
如果使用此字符串,它将报告2

$string = 'This string contains a string and a other words!';
最重要的是,如果您使用包含单词
string
的字符串两次,而不是所需单词中的两个,则只报告1

$string = 'This string contains a string and other stuff!';

尝试
preg_match('~\b(?:'.introde('.|',$arr.))\b.*\b(?:'.introde('.|',$arr.))\b~,$string)
Try
substr_count
嘿@MA,但是如何对多针使用
substr_count
(不区分大小写):-s。例如,我想检查
字符串:这是我的和你的
arr:is和
因此,如果
在该字符串中找到,它将返回true:-shey@WiktorStribiżew你能帮我了解那里发生了什么吗?:-sTry
preg_match('~\b(?:'.introde('.|',$arr.))\b.*\b(?:'.introde('.|',$arr.))\b~,$string)
Try
substr_count
hey@MA,但是如何对多个针使用
substr_count
(不区分大小写):-s。例如,我想检查
字符串:这是我的和你的
arr:is和
因此,如果
在该字符串中找到,它将返回true:-shey@WiktorStribiżew你能帮我了解那里发生了什么吗?:-如果字符串包含
“此字符串包含一个字符串”
它也将返回2,因为有2个
字符串
单词。但是它不包含至少2个您正在查找的单词,它只会查找
字符串
两个,除非字符串包含
“此字符串包含字符串”
,它还将返回2,因为有2个
字符串
单词。但是它不包含至少2个您正在查找的单词,它只会查找
字符串
两个。如果字符串是
字符串,并且字符串
@riggsfully code已更新,则会出现问题。现在,它将检查唯一值这有一个问题,如果字符串是
字符串和
@riggsfully-code-updated字符串。现在它将检查唯一值