坏词正则表达式过滤器无法正常工作(Bad words regex filter not working)

编程入门 行业动态 更新时间:2024-10-28 05:24:53
坏词正则表达式过滤器无法正常工作(Bad words regex filter not working)

我试图让一个坏词过滤器工作。 到目前为止,使用下面的代码,如果我输入下面数组中列出的“bad1”这样的坏词,则不会发生过滤,我收到此错误:

警告:preg_match()[function.preg-match]:未知的修饰符'/'

这是代码:

if (isset($_POST['text'])) { // Words not allowed $disallowedWords = array( 'bad1', 'bad2', ); // Search for disallowed words. // The Regex used here should e.g. match 'are', but not match 'care' foreach ($disallowedWords as $word) { if (preg_match("/\s+$word\s+/i", $entry)) { die("The word '$word' is not allowed..."); } } // Variable contains a regex that will match URLs $urlRegex = '/(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0- 9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9] {1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1 -9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0) \.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost |([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\. (com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-z A-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*/'; // Search for URLs if (preg_match($urlRegex, $entry)) { die("URLs are not allowed..."); } }

I'm trying to get a bad words filter to work. So far, with the code below, no filtering happens if I type a bad word like "bad1" listed in array below and I get this error:

Warning: preg_match() [function.preg-match]: Unknown modifier ‘/’

Here is the code:

if (isset($_POST['text'])) { // Words not allowed $disallowedWords = array( 'bad1', 'bad2', ); // Search for disallowed words. // The Regex used here should e.g. match 'are', but not match 'care' foreach ($disallowedWords as $word) { if (preg_match("/\s+$word\s+/i", $entry)) { die("The word '$word' is not allowed..."); } } // Variable contains a regex that will match URLs $urlRegex = '/(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0- 9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9] {1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1 -9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0) \.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost |([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\. (com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-z A-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*/'; // Search for URLs if (preg_match($urlRegex, $entry)) { die("URLs are not allowed..."); } }

最满意答案

这是匹配单词的正确方法。 在foreach循环中使用此正则表达式。

preg_match("#\b" . $word . "\b#", $entry);

您还可以在此处测试正则表达式。 使用/\bbad1\b/g 。

代码投入使用:

<?php // delete the line below in your code $entry = "notbad1word bad1 bad notbad1."; $disallowedWords = array( 'bad1', 'bad2', ); foreach ($disallowedWords as $word) { // use $_POST['text'] instead of $entry preg_match("#\b". $word ."\b#", $entry, $matches); if(!empty($matches)) die("The word " . $word . " is not allowed."); } echo "All good.";

此代码与notbad1word或notbad2word (等等)不匹配,但仅匹配bad1或bad2 。

对于你的urlRegex,你必须像这样转义/ \ : \/

This is the right way of matching the words. Use this regex in your foreach loop.

preg_match("#\b" . $word . "\b#", $entry);

You can also test your regular expressions here. Use /\bbad1\b/g.

The code put in action:

<?php // delete the line below in your code $entry = "notbad1word bad1 bad notbad1."; $disallowedWords = array( 'bad1', 'bad2', ); foreach ($disallowedWords as $word) { // use $_POST['text'] instead of $entry preg_match("#\b". $word ."\b#", $entry, $matches); if(!empty($matches)) die("The word " . $word . " is not allowed."); } echo "All good.";

This code doesn't match notbad1word or notbad2word (and so on) but matches only bad1 or bad2.

As to your urlRegex, you have to escape / with \ like this: \/

更多推荐

本文发布于:2023-08-04 15:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417764.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   无法正常   工作   正则表达式   Bad

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!