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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.