我在php中实现了这个“坏词”检查功能:
# bad word detector
function check_badwords($string) {
$badwords = array(a number of words some may find inappropriate for SE);
foreach($badwords as $item) {
if(stripos($string, $item) !== false) return true;
}
return false;
}
它工作正常,除了我有一个小问题。如果 $string 是:
Who is the best guitarist ever?
...它返回 true,因为与Who ($string) 和ho (在 $badwords 数组中) 匹配。如何修改该函数以使其仅检查完整的单词,而不仅仅是单词的一部分?
- check_badwords('她是个豪'); //应该返回真
- check_badwords('她是谁?'); //应该返回false
谢谢!