我们中的许多人都需要处理用户输入、搜索查询以及输入文本可能包含亵渎或不受欢迎的语言的情况。通常情况下,这需要过滤掉。

在哪里可以找到各种语言和方言的粗口?

源代码中是否有包含好的列表的api ?或者可能是一个API,用一些参数简单地说“是的,这是干净的”或“不,这是脏的”?

有什么好方法可以捕捉那些试图欺骗系统的人,比如$$、azz或a55?

如果您为PHP提供了解决方案,则会有加分。:)

编辑:回答简单地说,避免程序问题:

我认为,当用户可以使用公共图像搜索来查找添加到敏感社区池的图片时,这种过滤器是有一席之地的。如果他们可以搜索“阴茎”,那么他们可能会得到很多照片,是的。如果我们不想要照片,那么阻止这个词作为搜索词是一个很好的把关者,尽管不可否认这不是一个万无一失的方法。真正的问题是首先获得单词列表。

我指的是一种方法来判断单个令牌是否脏,然后简单地禁止它。我不会费心去阻止那种完全滑稽的“长脖子长颈鹿”的说法。你在那里什么也做不了。:)


当前回答

我不知道有什么好的库可以做到这一点,但无论你做什么,都要确保你在让东西通过的方向上犯了错误。我曾经遇到过不允许我使用“mpassell”作为用户名的系统,因为它包含“ass”作为子字符串。这是一种疏远用户的好方法!

其他回答

防止攻击性用户输入的唯一方法是阻止所有用户输入。

如果你坚持允许用户输入,并且需要审核,那么就加入人工审核员。

关于“欺骗系统”子问题,您可以在进行搜索之前通过规范化“坏词”列表和用户输入的文本来处理它。例如,使用一系列正则表达式(如果PHP有,可以使用tr)将[z$5]转换为“s”,[4@]转换为“a”,等等,然后将规范化的“坏词”列表与规范化的文本进行比较。请注意,规范化可能会导致额外的假阳性,尽管目前我想不出任何实际情况。

更大的挑战是想出一种方法,让人们在引用“笔比剑更强大”的同时屏蔽“pen is”。

我有点晚了,但我有一个解决方案,可能对一些读到这篇文章的人有用。它是用javascript而不是php编写的,但这是有原因的。

完全披露,我写了这个插件…

不管怎样。

我采用的方法是允许用户“选择加入”他们的亵渎过滤。基本上,默认情况下允许使用脏话,但如果我的用户不想读,他们也可以不读。这也有助于解决“l33t sp3@k”问题。

这个概念是一个简单的jquery插件,如果客户端的帐户启用了脏话过滤,服务器就会注入这个插件。从这里开始,就只有几行简单的文字,用来掩盖誓言。

这是演示页面 https://chaseflorell.github.io/jQuery.ProfanityFilter/demo/

<div id="foo">
    ass will fail but password will not
</div>

<script>
    // code:
    $('#foo').profanityFilter({
        customSwears: ['ass']
    });
</script>

结果

***将失败,但密码不会

注意本地化问题:在一种语言中是脏话的词在另一种语言中可能是完全正常的词。

目前的一个例子是:ebay使用字典的方法从反馈中过滤“坏词”。如果你试图输入德语翻译的“this was a perfect transaction”(“das war eine perfekte Transaktion”),ebay将会因为用词不当而拒绝反馈。

为什么?因为“was”的德语单词是“war”,而“war”在ebay的“坏词”词典中。

所以要注意本土化问题。

一旦你有了一个好的MYSQL表,里面有一些你想要过滤的坏单词(我从这个线程中的一个链接开始),你可以这样做:

$errors = array();  //Initialize error array (I use this with all my PHP form validations)

$SCREENNAME = mysql_real_escape_string($_POST['SCREENNAME']); //Escape the input data to prevent SQL injection when you query the profanity table.

$ProfanityCheckString = strtoupper($SCREENNAME); //Make the input string uppercase (so that 'BaDwOrD' is the same as 'BADWORD').  All your values in the profanity table will need to be UPPERCASE for this to work.

$ProfanityCheckString = preg_replace('/[_-]/','',$ProfanityCheckString); //I allow alphanumeric, underscores, and dashes...nothing else (I control this with PHP form validation).  Pull out non-alphanumeric characters so 'B-A-D-W-O-R-D' shows up as 'BADWORD'.

$ProfanityCheckString = preg_replace('/1/','I',$ProfanityCheckString); //Replace common numeric representations of letters so '84DW0RD' shows up as 'BADWORD'.

$ProfanityCheckString = preg_replace('/3/','E',$ProfanityCheckString);

$ProfanityCheckString = preg_replace('/4/','A',$ProfanityCheckString);

$ProfanityCheckString = preg_replace('/5/','S',$ProfanityCheckString);

$ProfanityCheckString = preg_replace('/6/','G',$ProfanityCheckString);

$ProfanityCheckString = preg_replace('/7/','T',$ProfanityCheckString);

$ProfanityCheckString = preg_replace('/8/','B',$ProfanityCheckString);

$ProfanityCheckString = preg_replace('/0/','O',$ProfanityCheckString); //Replace ZERO's with O's (Capital letter o's).

$ProfanityCheckString = preg_replace('/Z/','S',$ProfanityCheckString); //Replace Z's with S's, another common substitution.  Make sure you replace Z's with S's in your profanity database for this to work properly.  Same with all the numbers too--having S3X7 in your database won't work, since this code would render that string as 'SEXY'.  The profanity table should have the "rendered" version of the bad words.

$CheckProfanity = mysql_query("SELECT * FROM DATABASE.TABLE p WHERE p.WORD = '".$ProfanityCheckString."'");
if(mysql_num_rows($CheckProfanity) > 0) {$errors[] = 'Please select another Screen Name.';} //Check your profanity table for the scrubbed input.  You could get real crazy using LIKE and wildcards, but I only want a simple profanity filter.

if (count($errors) > 0) {foreach($errors as $error) {$errorString .= "<span class='PHPError'>$error</span><br /><br />";} echo $errorString;} //Echo any PHP errors that come out of the validation, including any profanity flagging.


//You can also use these lines to troubleshoot.
//echo $ProfanityCheckString;
//echo "<br />";
//echo mysql_error();
//echo "<br />";

我确信有一种更有效的方法来完成所有这些替换,但我不够聪明,无法找到它(尽管效率不高,但这似乎还可以)。

我认为你应该允许用户注册,并根据需要使用人工过滤和添加到你的亵渎表。尽管这一切都取决于一个假阳性(好的词被标记为坏)和一个假阴性(坏词通过)的成本。这将最终决定您在过滤策略中是积极还是保守。

如果您想使用通配符,我也会非常小心,因为它们有时会比您想要的更麻烦。