检测是否提到了一组单词

时间:2017-02-01 19:02:06

标签: php filter

我想知道是否还有检测是否在 PHP 的字符串中提到了一组单词。让我们假设它可以说"突击"但是"突击+步枪" ISN'吨。所以......

"I saw an assault"不应该被发现

"I bought an assault rifle"应该

"I assaulted a bank with my rifle"也应该

1 个答案:

答案 0 :(得分:2)

一种常见的方法是将输入字符串(即"assault+rifle")拆分为"+" s,然后对于每个输入字符串,检查它是否在原始文本中。

// Assuming:
$text;
$words;

// Iterating through the splitted $words
foreach(explode("+", $words) as &$word) {
    // Checking if the text contains our word
    if (strpos($text, $word) === false)) {
        die("Allowed");
    }
}
echo "Not Allowed";