如何在不影响单词的情况下过滤掉粗鲁的单词。在我的例子中,由于草中的“屁股”,草一词正在被审查。如何更改此代码,以便用 * *替换屁股,但草不受影响?
$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 * * 传播后切,并在几天后观察差异
答案 0 :(得分:0)
您需要检查相关字符串之前和之后的空格。
示例:
<?php
$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
foreach ($naughtyWords as &$word) {
$word=' '.$word.' ';
}
$string = str_replace($naughtyWords, " **** ", ' '.$string.' ');
echo $string;
?>
答案 1 :(得分:0)
我认为戴夫的说法是:
您需要在每个单词之前检查空格。
$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;
或者更好的是:
$string 'cut the grass spread the aftercut and watch the difference after a few days';
$patterns = array('/ahole/','/anus/','/ash0le/','/ash0les/','/asholes/','/ass/','/Ass Monkey/','/Assface/');
$string = preg_replace($patterns, '****', $string);