高级Word过滤器

时间:2012-08-10 03:04:30

标签: php

我的剧本真的需要一个好的坏词过滤器。我已经有了一系列坏话。但我的问题是它检测到了坏词,换句话说就像说“测试”是一个坏词而你说“测试”它仍然会把它算作一个诅咒词。在我的第二次尝试中,我修改了检测词,换句话说,人们可以通过说@test和'cusswordhere'来绕过它等。

我有什么办法可以解决这个问题吗?

我的代码:

  function censor($message) {
            $badwords = $this->censor; //array containing bad words
            $message = @preg_replace('[^A-Za-z0-9 ]','',strtolower(' '.$message.' '));
            foreach($badwords as $bad) {
                $bad = trim($bad);
                if(strpos($message.' ', $bad.' ')!==false) {
                    if(strlen($bad)>=2) {
                        return true;
                    }
                }   
            }

    }

目前它没有检测到其他单词中的单词,但你可以通过说@cusshere“cusshere”之类的东西来绕过它,有什么办法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这将在列表中捕获任何出现的作品,无论它出现在消息中的哪个位置;可能过于严格,但可能会有所帮助:

function censor($message) {
  foreach($this->censor as $word){
    if(false !== stripos($message, $word)){
      return true;
    }
  }
  return false;
}
相关问题