【问题标题】:php bad word content filterphp坏词内容过滤器
【发布时间】:2017-03-01 00:18:48
【问题描述】:

我有一个可以工作的内容过滤器,但它并没有像我想要的那样工作。 例如,如果我说“badword you i badword hat you”,则整个内容输出为“[CONTENT DELETED]”,但我希望它说“******* you i ******* hat 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;
}
?>

【问题讨论】:

  • 这是一项不可能完成的任务,你可以捕捉到不好的词(使用很长的字典),但你无法捕捉到这些词的所有变体:FuUuCk, @ 987654323@... 简而言之,尝试这样做只是浪费时间,并不能取代版主。新闻:我们现在是2017年,您可以毫无风险地使用所有mysql_功能。
  • 我知道我不能阻止他们,那不是我的问题......
  • 也许,但这是我的答案。

标签: php arrays mysqli


【解决方案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 ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-11
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 2021-06-14
    • 2022-10-24
    相关资源
    最近更新 更多