检测字符串中混淆的脏话

时间:2016-07-27 13:42:30

标签: php

我想在PHP中使用一个函数或方法来检测模糊文本中的脏话。 会检查字符串的东西:

$string = "hey you swearword!" or 
$string = "hey you swear#word!"

或者甚至

$string = "hey you sw3arw0rd!"

表示“swearword”,如果它包含那个坏的脏话,则返回true,如果不包含,则返回false。 我不希望别人在我的网站上使用坏词,请帮忙!

1 个答案:

答案 0 :(得分:0)

只是一个显示方向的简单示例:

$stopwords = ['swearword'];

$test = ['swear#word','sw3arw0rd','goodword','swearw*rd','swe*rw*rd','swe*!**rd'];

foreach($test as $word){
    foreach($stopwords as $stopword){
        if(levenshtein($stopword,$word)<=2){
            print "levenshtein: '$word' seems to mean $stopword<br/>";
            continue 2;
        }
    }
    if(strlen(preg_replace('#[a-zA-Z]+#','',$word))!==0){#special char found
        print "preg_replace: '$word' seems to have illegal chars<br/>";
        continue;
    }
    print "'$word' seems be NO stopword<br/>";
}