我怎样才能过滤掉粗鲁的词而不影响可以的词。下面在我的示例中,由于草中的“屁股”,草这个词被审查了。如何更改此代码,以便将 ass 替换为* * 但草不受影响?
$string = 'cut the grass spread the aftercut and watch the difference after a few days';
$naughtyWords = array("ahole","anus","ash0le","ash0les","asholes","ass","Ass Monkey","Assface"); //etc
$string = str_replace($naughtyWords, "****", $string);
echo $string;
结果是
切 gr* *传播后切并观察几天后的差异