Remove bad words from posted form data that match records in MySQL table.

I have a table with a list of kewords or phrases that will contain bad or swear words.

  `keywords` text
INSERT INTO `bad_words` (`keywords`) VALUES
('badword1\r\badword2\r\bad phrase1\r\bad phrase2');

What is the easiet way to check posted data for bad words and if found remove them.

Say for example
$_POST[‘description’] = “this contains badword1 and bad prases2”;
$_POST[‘itemname] = “badword1 is in name”;

Also, should I be storing the words or phrases differently in the table, perhaps one word or phrase per record, or perhaps in an array?

Thanks in advance for your help.
Who is Participating?

Please check the following function:
function wordcensor($txt)

//REMEMBER to connect to mysql here

$q = mysql_query("SELECT `swears`, `replace` FROM `badword`");
while ($row = mysql_fetch_array($q))
$txt = str_ireplace($row['swears'], $row['replace'], $txt); 

return $txt;

Open in new window

Shinesh PremrajanEngineering ManagerCommented:
i think since you are storing the values as text. you need to do a Full text searching.

Refer this Link, Its not the excactly the same but this is what you are suppose to do for searching.

Hope this helps
Ray PaseurCommented:
@sabecs: Please let me suggest a different design pattern, rather than trying to make a dirty-word filter.  The reason for this suggestion is that a dirty-word filter is like a spam filter - in other words, it is a gigantic application, full of complex issues, with an ever-changing array of inputs.  Consider this string:

Go F*ck Yourself, Jack!

The obvious four-letter word will not be caught in any kind of text search, but the nasty intent is immediately obvious to the reader.  So the question should really be changed from "What words do you want to filter?" to something more like, "Which inputs will I trust?"

Ways of getting trusted inputs usually include a design that only allows registered and logged in clients to post messages to public pages, or has a moderator check.  An additional way to handle this is to use the ubiquitous "report this post" link.  This latter method allows your audience to establish its own standards of decency.

Best of luck with your project, ~Ray
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

i had to do something similar not long ago. my advice:

1) do not store text, but regexes (if you don't know abuot regexes, now it's a good time to learn, then check preg_replace() )
2) store (persist) the list in a table, one regex per line, but if you get many requests (posts), consider and in memory array
3) be a little permissive!
sabecsAuthor Commented:
Thanks, works great.
sabecsAuthor Commented:
Thanks for your feedback, I will also be placing a "report this post" as per Ray's suggestion but I just need something to remove simple bad words from a list.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.