PHP函数检查违禁单词 [英] PHP function to check for banned words
本文介绍了PHP函数检查违禁单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有此功能来检查字符串中是否有禁止的单词.但是,如果删除空格,我将无法检测到禁用词-导致该功能无用.有人可以帮助我改进它来检测没有空格的单词.
I have this function to check for banned words in my string. However, I cannot detect banned words if the spaces are removed - rendering the function useless. Can someone help me improve it to detect the words without spaces.
$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;
}
推荐答案
\b
检测到单词边界,将其删除以得到常规匹配.
\b
detects word boundaries, remove them to get a regular match.
"/(" . implode("|", $badWords) . ")/i",
这篇关于PHP函数检查违禁单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文