Search code examples
phpmysqlregexblacklist

Blacklisting words that can be entered into a MySQL database via a php form using regex


Okay, at the moment on my site I have a feature that blacklists words, and if it detects them when the form is submitted, the post is not submitted, here it is:

$disallowedWords = array(
'list','of','bad','words'
);

foreach ($disallowedWords as $word) {
  if (preg_match("/\s+$word\s+/i", $entry)) {
    die('The word or phrase ' . $word . ' is not allowed...');
  }
}

$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-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*';
if (preg_match($urlRegex, $entry)) {
  die('The word or phrase ' . $word . ' is not allowed...');

} 

This works for sentences like this:

Here is a list

It would stop the post and say the word list wasn't allowed, however if I put:

here is a listt

or

here is alist

It doesn't work, so, how (if it's even possible) could I make it censor that exact letter combination? Or whatever would work to the effect that I couldn't enter 'listt' or 'alist' etc.


Solution

  • Removing the two instances of '\s+' from your Regex will do what you're asking, but it will also blacklist any (potentially valid) word that contains a 'bad word'