php坏词内容过滤器

时间:2017-03-01 00:18:49

标签: php arrays mysqli

我有一个有效的内容过滤器,它只是不像我想要的那样工作。 例如,如果我说“badword you the badword hate you”整个事情被输出为“[删除内容]”但我希望它说“*******你我害怕你“

<?php

function SecurePost($var) {
  return trim($DBcon->real_escape_string($var));
}

function filter($string) {
  $words = array("badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere", "badwordhere");
  $new =   array("****", "*****", "***", "*******", "****", "****", "*****", "******", "*****", "******", "*****", "***", "*******", "***", "****", "***", "*****", "*******", "****", "****", "*****", "****", "****", "****", "******");
  $string1 = str_ireplace($words, $new, $string);
  $findme = "***";
  $pos = strpos($string1, $findme);
  if ($pos !== false) {
    $string1 = "[ Content Deleted ]";
    return $string1;
  }
  else {
    return $string1;
  }
}

function SecureString($value) {
  $value = mysql_real_escape_string(stripslashes(strip_tags($value)));
  return $value;
}
?>

1 个答案:

答案 0 :(得分:1)

代码中的错误很少。请查看以下工作示例:

<?php
$str = "Kiss my badwordhere";
echo filter($str) . "\n";

$str = "badwordhere anywherebadword";
echo filter($str) . "\n";


function filter($string) {
  $words = array("badwordhere", "anywherebadword");
  $new =   array("****", "*******");
  $string1 = str_ireplace($words, $new, $string);
  $left = preg_replace("/[\* ]+/", "", $string1);
  if (strlen($left) == 0) {
    $string1 = "[ Content Deleted ]";
    return $string1;
  }
  else {
    return $string1;
  }
}
?>

打印

Kiss my ****
[ Content Deleted ]