Filter Bad Words Using PHP

Today we will learn how to filter bad words from text.

Many websites now filter out bad words from the comment fields and other places. We will go over how to filter out bad words from text using PHP.


Here is an example of text containing “bad words” that we want filtered:

Create an array() of bad words to be filtered out of the text.

Create an array() of the replacement words. You can change the replacement words to whatever your liking is.

We use the PHP str_ireplace function to replace the bad words.

The whole PHP code

Text without the filter: You’re a badword. You big jerk. Darn! Text with the filter: You’re a @#$@#. You big j&*%. Da*@!


A simple way to filter out bad words.
I hope this helps you.


Tagged with:

View all contributions by


Related Articles


Stay connected with us

Add us on Google+

Follow us on Facebook

Follow us on Twitter

Get updates via RSS

Get email updates


      1. Swathi Reddy

        When you come across a new bad word, then you have to edit your php file and the array values. With this approach you might mess up with your code.

        With database, you just have to insert one record which is a clean way to do it.

Leave a Reply

Your email address will not be published. Required fields are marked *