我有以下代码可以从字符串中过滤掉粗鲁的单词并将它们替换为askerisks,但是我希望askerisks 的数量等于粗鲁单词中的字母数。例如,如果“屁股”这个词被审查,那么它将被三个问号代替。如何修改此代码以实现此目的?谢谢。
$naughtyWords = array("ahole","anus","ash0le","ash0les","asholes","ass"); //etc
foreach ($naughtyWords as &$word) {
$word = ' '.$word.' ';
}
$string = str_replace($naughtyWords, " **** ", ' '.$string.' ');