Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Badwords-阻止用户绕过php中的坏词过滤器_Php_Regex - Fatal编程技术网

Badwords-阻止用户绕过php中的坏词过滤器

Badwords-阻止用户绕过php中的坏词过滤器,php,regex,Php,Regex,我使用badwordfilter数组来防止用户生成的内容中出现坏词 坏话的例子: $badwords = array ( 1 => 'adult', 2 => 'porn' ) 问题: a.dult po,rn 用户总是试图避免使用带有点、逗号或其他字符(如*!)的坏字过滤器# 示例: a.dult po,rn 是否有可能检查字符串/坏字中的字符列表(,?!),如果找到一个单词,其中一个字符在其中,则给出错误消息 "Don´t bypass the badword filter

我使用badwordfilter数组来防止用户生成的内容中出现坏词

坏话的例子:

$badwords = array ( 1 => 'adult', 2 => 'porn' )
问题:

a.dult
po,rn
用户总是试图避免使用带有点、逗号或其他字符(如*!)的坏字过滤器#

示例:

a.dult
po,rn
是否有可能检查字符串/坏字中的字符列表(,?!),如果找到一个单词,其中一个字符在其中,则给出错误消息

"Don´t bypass the badword filter".
非常感谢您抽出时间


我知道有很多方法可以绕过badword过滤器,但检查字符串中是否有点或逗号就足够了。

如果MySQL中有一个表,其中有一个排除的单词列表,则可以运行如下查询:

SELECT word, COUNT(word) AS matches 
FROM badwords 
WHERE LOWER('User input string goes here after processing') LIKE CONCAT('%', word, '%') 
GROUP BY word
这将返回一个已排除单词的列表及其计数

我所说的处理过程可以简单到:

preg_replace('/[^a-zA-Z0-9]/', '', $input)

要删除所有特殊字符,或者您可以尝试将字符替换为预期的替换字符,例如0的o等。这是一场针对具有ASCII创造性的人的替代复杂性战争。这不是一场真正可以获胜的战争,但你可以减少常见的选择。

Regex
\b
(坏词)
\b
[,?!]

详情:

  • \b
    在单词边界处断言位置
  • []
    匹配列表中的单个字符
PHP代码

$str = 'bla bla p.orn, bla bla a.dult bla bla association';

$bad_words = array('adult', 'porn', 'ass');
$reg = '~\b' . implode('\b|\b', $bad_words) . '\b~';

preg_match_all($reg, preg_replace('~[.,?!]~', '', $str), $matches);

if(count($matches[0]) > 0)
    echo '"Don´t bypass the badword filter".';
输出:

Array
(
    [0] => porn
    [1] => adult
)

您可以使用以下命令构造函数:


除去字母和空格之外的所有内容,检查是否有不好的单词。如果没有找到,则使用原件。然而,坏词过滤器并不是那么准确<代码>成人
可以合法使用,不是吗?另外,如果您阻止
ass
那么
association
呢?问题是您也可以使用p0rn-我从来没有这样做过:-/我知道wordfilter从来都不是100%。对于替换,我使用另一个数组。目前,我只是试图阻止用户在这个.Cl***ic和con****的问题上这么做。是的,有很多方法可以通过Bass来识别坏词,但对我来说,检查字符串中是否有点或逗号是可以的。这是非常聪明的@S.Jovan。当坏单词或字符串中的单词内有点时,是否也可以回显错误消息?谢谢。这几乎是完美的。当删除字符串中的点时,它也会给出错误消息?@labu77删除点时,我没有收到任何错误消息。。。你自己看吧,我看“不要绕过坏词过滤器”。在stdoutI上有PHP5.6。这可能是原因吗?如果能给我一个5.6版本的代码,那就太好了。我会试试的。我必须先创建一个表。谢谢哇,这也太聪明了。非常感谢@jan。我将测试所有建议。