Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to censor a string.



10:21 pm on Feb 5, 2008 (gmt 0)

5+ Year Member

When people submit a form, I'd like to check if that string they submit include some unfavourable words.

For example, if username= "hello#*$!lovely", only "#*$!" is unfavourable, but "hello" and "lovely" are surely not in the banned list. Which function can i use to check it?

Thanks a lot!


10:27 pm on Feb 5, 2008 (gmt 0)

5+ Year Member

ereg, its quite simple to do.

Check out the info at PHP.net

-ereg function [php.net]
-eregi function [php.net] (not case sensitive)

[edited by: GamingLoft at 10:28 pm (utc) on Feb. 5, 2008]


10:28 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You can search with regular expressions [php.net] or a simple strpos [php.net]/stripos [php.net] call on the string to find any bad words. These words are usually stored somewhere like a database, however, you can just as easily keep these in an array in your application.


10:34 pm on Feb 5, 2008 (gmt 0)

5+ Year Member

I usually just stick mine in a text file and then read them into the program. That way if somebody gets extra creative all I have to do to add a new blocked word is open the text file and add a line.


2:46 pm on Feb 6, 2008 (gmt 0)

5+ Year Member

The problem is where to get the list :( Found some entries searching for "bad words list".

Here is related topic [webmasterworld.com...]


5:29 pm on Feb 6, 2008 (gmt 0)

5+ Year Member

Thanks for all your help. As a newbie to PHP I'm still struggling to find out a solution to that. After reading the php manual provided above, Strpos seems to be the easiest way for me to go at this moment.

Here's my code...
Form :
<form method="POST" action="$_SERVER[PHP_SELF]">

Your name: <input type="text" name="username">
<input type="submit" value="submit">
$censor_word = 'fuxxk'
$test = strpos($username, $censor_word);

if ($test === true) {
echo "Don't call yourslef '$censor_word'. Use a proper name";


It seems fairly inefficient with this method. I'll try to use array to include more censored words (use foreach function?) .

I'd like to follow Duskrider's suggestion to use a text file to include dozens of bad words in one file, but have no idea how to do it yet. I appreciate any hints for this.



12:09 pm on Feb 8, 2008 (gmt 0)

5+ Year Member

Other way is to use a str_replace method if you dont want to do all that, but you will have to check one "bad word" at a time.

Ex $username = str_replace("%","",$username);

This will check the string for the % sign and replace it with nothing.


2:25 pm on Feb 8, 2008 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

str_replace also supports arrays:

$string = str_replace(array('bad','bad2','bad3'),'',$string);



3:35 pm on Feb 8, 2008 (gmt 0)

5+ Year Member

Here's how you would incorporate that with an external text file:

// Read the contents of 'badwords.txt' into an array
// trimming the newline characters as we do so.
$file = array_map('rtrim',file('badwords.txt'));

// Now just like Dreamcatcher said:
$string = str_replace($file,'*',$string);

That should look through your string and replace anything that looks like a line from the textfile with asterisks(*). If you wanted to replace it with something else, you could do that. If you don't care about case (you want to block Badword and badword and BaDWoRd but don't want to list each one in your text file individually) you should use str_ireplace instead.


4:49 pm on Feb 9, 2008 (gmt 0)

5+ Year Member

Amazing explanations. Every time i thought i have learned enough syntax and function () in php to accomplish a particular task, I always found that i still have a lot to learn. Thanks for everyone's contribution and i begin to love programming.

Featured Threads

Hot Threads This Week

Hot Threads This Month