MaryBaker MaryBaker - 1 year ago 63
HTML Question

Alternative way in php for if statement with too many "OR" ||

I've got this if statement that checks for these "Offensive words" in an input of types = text

if(strstr($key,'bi**h') || strstr($key,'fu**k') || strstr($key,'son of a bit**') || strstr($key,'cun*') || strstr($key,'fuc**r') || strstr($key,'mother fuc**') || strstr($key,'shi**') || strstr($key,'cr**p') ){

$erros['insult']='please avoid any offending words';


This code is made by another developer, I was just wondering is there any more way other than adding the double pipes for each word I have to enter?. It kinda looks a primitive way of writing such a code, I guess.

Answer Source

Be careful with bad word filters, what about the word shitake? Here's a simple way to replace bad words and check against the original string:

$bad = array('bi**h', 'fu**k', 'son of a   bit**', 'cun*', 'fuc**r', 'mother fuc**', 'shi**', 'cr**p');

if(str_replace($bad, '', $key) != $key) {
      $erros['insult']='please avoid any offending words';

However the if and || approach will be faster.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download