PHP函数来检查被禁止的单词

PHP函数来检查被禁止的单词,php,function,Php,Function,我有这个函数来检查我的字符串中的禁用词。但是,如果空格被删除,我就无法检测到被禁止的单词,这使得函数变得无用。有没有人能帮我改进一下,让我检测出没有空格的单词 $string = "Hellomynameisuser."; check_for_banned_words($string); function check_for_banned_words($string){ $badWords = array( "ban", "bad", "

我有这个函数来检查我的字符串中的禁用词。但是,如果空格被删除,我就无法检测到被禁止的单词,这使得函数变得无用。有没有人能帮我改进一下,让我检测出没有空格的单词

$string = "Hellomynameisuser.";
check_for_banned_words($string);

function check_for_banned_words($string){
    $badWords = array(
        "ban",
        "bad",
        "user",
        "pass",
        "stack",
        "name",
        "html"
    );
    $matches = array();
    $matchFound = preg_match_all(
        "/\b(" . implode($badWords,"|") . ")\b/i", 
        $string, 
        $matches
    );
    if($matchFound):
        $words = array_unique($matches[0]);
        echo("<ul>");
        foreach($words as $word):
            echo("<li>" . $word . "</li>");
        endforeach;
        echo("</ul>");
    endif;
}
$string=“Hellomynameisuser。”;
检查是否存在禁止的单词($string);
函数检查\u以查找\u禁止的\u字($string){
$badWords=数组(
“禁止”,
“坏”,
“用户”,
“通行证”,
“堆栈”,
“姓名”,
“html”
);
$matches=array();
$matchFound=preg\u match\u all(
“/\b(“.introde($baddwords,”)\b/i”,
$string,
$matches
);
如果找到($matchfind):
$words=array_unique($matches[0]);
回声(
    ); foreach($words作为$word): 回声(“
  • ”$word.
  • ”); endforeach; 回声(“
”); endif; }
\b
检测单词边界,删除它们以获得常规匹配

"/(" . implode("|", $badWords) . ")/i", 

问题!然而,如果你这样做,你也将禁止诸如“香蕉”和“即”之类的词语。这就是你想要的吗?@DanFromGermany,或者我们以前所说的斯肯索普问题,在我早期工作的时候,我曾在那里工作过非常糟糕的亵渎过滤器;)