【发布时间】:2017-11-07 09:33:22
【问题描述】:
以下php 函数被用于用开始替换坏词,但我需要一个额外的参数来描述是否找到坏词。
$badwords = array('dog', 'dala', 'bad3', 'ass');
$text = 'This is a dog. . Grass. is good but ass is bad.';
print_r( filterBadwords($text,$badwords));
function filterBadwords($text, array $badwords, $replaceChar = '*') {
$repu = preg_replace_callback(array_map(function($w) { return '/\b' . preg_quote($w, '/') . '\b/i'; }, $badwords),
function($match) use ($replaceChar) {
return str_repeat($replaceChar, strlen($match[0])); },
$text
);
return array('error' =>'Match/No Match', 'text' => $repu );
}// Func
如果找到的坏词应该像这样的输出
Array ([error] => Match[text] => Bad word dog match.)
如果没有找到坏词,那么
Array ([error] => No Match[text] => Bad word match.)
【问题讨论】:
-
在什么基础上可以判定字符串是坏词?
-
如果 $text 的任何单词与 $badwords 匹配,则表示该单词是错误的
-
那么你遇到了什么问题?
-
未能在“匹配/不匹配”的位置使用变量
-
您最好的解决方案始终是手动审核。没有完美的自动化解决方案(参见“斯肯索普问题”)