我有一个可以去除亵渎词的工作函数。str_replace匹配后跟空格或特殊字符
单词列表由1700个坏词组成。
我的问题是,它审查
'BADWORDS'
但不
'BADWORDS。' ,'坏词'等。
如果我选择后
$ BADWORD [$关键] = $字以除去空间;
代替
$ BADWORD [$关键] = $字“。“;
那么我将有一个更大的问题,因为如果不好的话是CON那么它会剥夺一个字恒
我的问题是,我怎么能剥夺一个字,接着除特殊字符空间?
badword。 badword#badword,
。
function badWordFilter($data)
{
$wordlist = file_get_contents("badwordsnew.txt");
$words = explode(",", $wordlist);
$badword = array();
$replacementword = array();
foreach ($words as $key => $word)
{
$badword[$key] = $word." ";
$replacementword[$key] = addStars($word);
}
return str_ireplace($badword,$replacementword,$data);
}
function addStars($word)
{
$length = strlen($word);
return "*" . substr($word, 1, 1) . str_repeat("*", $length - 2)." " ;
}
你不想使用正则表达式吗? – splash58
@ splash58只要它能工作,我就可以用它:) –
preg_replace()..它的工作为你尝试这个.. –