0

我怎样才能过滤掉粗鲁的词而不影响可以的词。下面在我的示例中,由于草中的“屁股”,草这个词被审查了。如何更改此代码,以便将 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* *传播后切并观察几天后的差异

4

2 回答 2

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;

?>
于 2013-05-30T22:22:10.290 回答
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);
于 2013-05-30T22:26:49.670 回答