Badwords are bad, how can i get rid of them?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Badwords are bad, how can i get rid of them?

Post by chris12295 »

i have a database table of bad words, how can i find all occurences of the words in a form text field and replace them with a blank?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you put all the bad words into an array you can use str_replace() to change any occurances to blanks:

Code: Select all

$badwords = array('foo', 'bar', 'ick');
$my_dirty_text = 'This is a foo bar ick day';
$my_clean_text = str_replace($badwords, '', $my_dirty_text);
echo $my_clean_text;
Outputs

Code: Select all

This is a  day
Mac
Post Reply