Link to home
Start Free TrialLog in
Avatar of Rosewell
Rosewell

asked on

Detect a bad words in PHP

Hello Experts;

I'm a newbie in PHP and knows nothing about it. I have this html code that
displays a form and a button. If I press on the button it shows a message
like "No Bad Words Allowed !".

<html>
<head>
<script language="JavaScript">
<!--  Begin
function askData() {
alert ("No Bad Words Allowed !"+"Sorry . . .");  
}
// End -->
</script></head>
<body><center>
<form>
<textarea name=comments rows=10 cols=50>
</textarea><br>
<input type=button value="Submit" onClick="askData()">
</form></center>
</body>
</html>

What I would like to do is to catch a bad words on the form, before it displays a message.
and prevent it to be submitted in my mysql database.

Can anyone complete this html code above and tract down the bad words and prevent it
from going to a mysql database, thanks in advance.

This is the senario :

If a user type in a bad word like "(Edited by Computer101)" on the form and press submit there will be a
message that will display my warning message and then prevent it from going to my
mysql database.

I'm gonna add an additional 100 pts. to anyone who can complete the codes.

SOLUTION
Avatar of y2kwacko
y2kwacko

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aolXFT
aolXFT

Personally, I think the thing should be handled at PHP Level, and not through HTML/JS, since turning off/circumventing JS is pretty easy.

Something like

<?php

function clean_bad_words($str){
  $bad_words = array("(Edited by Computer101)", "(Edited by Computer101)", "boss", "(Edited by Computer101)", "work", "wanker");
  foreach($bad_words as $bad_word){
    if(strpos($str, $bad_word)){
      die("We don't like bad words here, Sorry")
    }
  }
  return $str;
}

?>

Alternatively you could just filter them out with something like

<?php

function clean_bad_words($str){
$bad_words = array("(Edited by Computer101)", "(Edited by Computer101)", "boss", "(Edited by Computer101)", "work", "wanker")
return str_replace($bad_words, "{bad_lang}", $str);
}

?>
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rosewell

ASKER

Thanks a lot guys, I'm gonna try it all and pick the best solution :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial