0

I'm making a joomla module and wanto block or ban bad words in message textarea. I tried

if(preg_match('/adult|sex|porn/i',trim($_POST['cf_message']))){
 echo 'bad word, pls remove it';
    }

But I want to quote the bad words in a text field in module backend, I entered bad words in the text field:

adult|sex|porn

$badwords= $params->get("bad_words", " ");

I changed above codes to be:

if(preg_match(/$badwords/i,trim($_POST['cf_message']))){
 echo 'bad word, pls remove it';
    }

but couldn't work.

zhang
  • 1
  • 1
  • 1
    Why is `www` a bad word lol – ADyson Jun 22 '21 at 14:16
  • Anyway if `$badwords` contains `adult|sex|porn` then that will be your regular expression. But your original regular expression was `/adult|sex|porn|www/i` ...in other words you're trying to run it without the `/` at the start and without the `/i` at the end. You need to add those back in before passing the string to preg_match. – ADyson Jun 22 '21 at 14:17
  • 2
    BTW this approach is prone to false positives quite easily - see https://en.wikipedia.org/wiki/Scunthorpe_problem for information and examples. – ADyson Jun 22 '21 at 14:18
  • lots of spam messages contain www, so I want to ban www – zhang Jun 22 '21 at 14:21
  • 1
    But you'll also block people from posting legitimate links. – ADyson Jun 22 '21 at 14:23
  • Yes, I see @ADyson – zhang Jun 22 '21 at 14:29
  • if(preg_match('/$badwords/i',trim($_POST['cf_message']))){ echo 'bad word, pls remove it'; } I add / and /i back, but couldn't work – zhang Jun 22 '21 at 14:30
  • This rabbit hole is not worth going down. If someone wants to write `p0rn` (with a zero) or `po rn`, you are going to be playing "cat & mouse" with these bad posters until the end of time. If you are truly concerned about foul posts, then annoy suspected abusers and tell them that their posts will not be published until they are reviewed by a human. The bottom line is that the regex will either be inadequate or a nightmare to maintain. – mickmackusa Jun 25 '21 at 04:33
  • Topical: https://extensions.joomla.org/extension/bad-word-filter/ – mickmackusa Feb 17 '22 at 04:51

0 Answers0