坏词过滤器,如何按长度替换单词?

时间:2015-12-05 22:02:05

标签: php

我有这个PHP代码,它会产生一个坏词:

$wordreplace = array (
  "!",
  "#",
  "@",
  "&",
  "^",
  "$",
  "%",
  "*"
);

class badword {
  function word_fliter($content) {
    global $badwords, $wordreplace;
    $count = count($badwords);
    $countfilter = count($wordreplace);
    // Loop through the badwords array
    for ($n = 0; $n < $count; ++$n, next ($badwords)) {
      //Create random replace characters
      $x = 2;
      $y = rand(3,5);
      $filter = "";
      while ($x<="$y") {
        $f = rand(0,$countfilter);
        $filter .="$wordreplace[$f]";
        $x++;
      }

      //Search for badwords in content
      $search = "$badwords[$n]";
      $content = preg_replace("'$search'i","<i>$filter</i>",$content);
    }
    return $content;
  }
}

但是,我需要它来生成等于坏词中字母数量的星号。

2 个答案:

答案 0 :(得分:2)

你可能会过度思考这个问题。代码的整个第一部分是为了找到坏词的随机替换。只有最后两行很重要,您可以用以下代码替换它们:

$content = preg_replace_callback(
    $badwords,
    function ($matches) {
        return str_repeat('*', strlen($matches[0]));
    },
    $content
);

要做到这一点,你需要将你的坏词包装在一个'捕获组'中,如下所示:

$capture_badwords = [];
foreach ($word in $badwords) {
    $capture_badwords[] = "/(".$word.")/";
}

这应该产生一个这样的数组:

["/(abadword)/", "/(anotherbadword)/", "/(afourletterword)/", ... ]

preg_replace_callback允许您定义一个可以操作匹配组的函数。

将这个放在一个例子中:

$badwords = [ "/(dog)/", "/(cat)/", "/(here)/" ];
//"#\((\d+)\)#"
$phrase = "It is just dogs chasing cats in here.";

echo preg_replace_callback(
        $badwords,
        function ($matches) {
            return str_repeat('*', strlen($matches[0]));
        },
        $phrase
    );

收率:

  

这只是在****中追逐***。

答案 1 :(得分:2)

用于替换字符串中的错误单词,您可以使用preg_replace_callback执行正则表达式搜索并使用回调替换。

下面的例子用*

替换所有不良单词

输出:

  

PHP是一种特殊的通用脚本语言   适合web ***** opment。

<?php
header('Content-Type: text/plain');

// array of bad words to filter 
$badwords = array('pop','devel');

// strin to clean from bad words  
$text = 'PHP is a popular general-purpose scripting language that is especially suited to web development.';
// with /\w+/ regex we send each word to  replace_badwords function 
$text = preg_replace_callback('/\w+/', 'replace_badwords', $text);

echo $text;

// replace_badwords check if string has bad word then its replace with * char or not return otrginal word
function replace_badwords($ocr) {
  global $badwords;
  $newstr=false;
  foreach ($badwords as $key => $value) {

        $start = strpos($ocr[0],$value);
        if($start!== false){
            $length = strlen($value);
            $newstr = substr_replace($ocr[0],str_repeat("*",$length),$start,$length);
        }

        }

    return $newstr ? $newstr :  $ocr[0];

}

?>