0

文字列から失礼な単語を除外し、それらをアスケリスクに置き換える次のコードがありますが、アスケリスクの数を失礼な単語の文字数と等しくしたいと思います。たとえば、「お尻」という単語が検閲された場合、3 つのアスケリスクに置き換えられます。これを達成するためにこのコードを変更するにはどうすればよいですか? ありがとう。

$naughtyWords = array("ahole","anus","ash0le","ash0les","asholes","ass"); //etc

foreach ($naughtyWords as &$word) {
    $word = ' '.$word.' ';
}

$string = str_replace($naughtyWords, " **** ", ' '.$string.' ');
4

2 に答える 2

2

これを試して:

$naughty_words = array('ahole', 'anus', 'ash0le', 'ash0les', 'asholes', 'ass');
$string = 'classical music ass dirty ass. molass';

foreach ($naughty_words as $naughty_word) {
    $string = preg_replace_callback('#\b' . $naughty_word . '\b#i', function($naughty_word) {return str_repeat('*', strlen($naughty_word[0]));}, $string);
}
于 2013-05-31T08:38:10.707 に答える
0

試す:

$naughtyWords = array("ahole","anus","ash0le","ash0les","asholes","ass"); //etc

foreach ($naughtyWords as $word) {
    $replacement = str_repeat('*', strlen($word));
    $string = str_replace(' '.$word.' ', $replacement, $string);
}
于 2013-05-31T08:36:57.967 に答える