I'd like to make a word filter, where should I start?

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
Darkzero
Forum Newbie
Posts: 18
Joined: Thu Oct 25, 2007 8:48 pm

I'd like to make a word filter, where should I start?

Post by Darkzero »

I have some input forms that store data in my SQL database, I just want to filter those forms for certain cuss words and phrases that i'd rather not show up on my site. Can someone point me in the right direction?

Thanks much :]
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

just build an array

Post by rturner »

Code: Select all

<?php

$bad = array(" word1 ", " word2 "); # etc etc

foreach ($bad as $word) {
           $message = str_replace($word, "{edited}", strtolower($message));
}

?>
Darkzero
Forum Newbie
Posts: 18
Joined: Thu Oct 25, 2007 8:48 pm

Post by Darkzero »

okay thanks, ill give it a try. :]
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

this is better

Post by rturner »

The code I gave you earlier doesn't replace multiple occurrences of the same word within a variable so....

Code: Select all

<?php

$bad = array("word1", "word2"); # etc etc

foreach ($bad as $word) {
        $message = preg_replace("/$word/i", '{edited}', $message);
}

?>
So with the other example if word1 was present more than once it would not be replaced the second and ensuing times.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Are you sure, rturner? I have a php that emboldens every occurance of a given word, and it uses str_replace. :?:
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

like so:

Code: Select all

$word = "bob";
$phrase = "Hello, bob.  How are you?";
$phrase = str_replace('bob', '<strong>bob</strong>', $phrase);
echo $phrase;
outputs Hello, bob. How are you?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

The docs for str_replace() show that both the search & replacement arguments can be arrays - so there's no need to do a foreach loop:

Code: Select all

<?PHP

$bad = array('bad1',
	     'bad2',
	     'bad3');

$input = 'I am a bad1ing mother bad3';
$clean_input = str_replace($bad,'{edited}',$input);

//$clean_input now = I am a {edited}ing mother {edited}

?>
You can make the 2nd argument an array as well & replace particular words with particular replacements, rather than a blanket replacement for all bad words.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Personally, if someone were to put an explanative in my website, I would eliminate it totally. :idea:
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

Post by rturner »

pickle's right . no need for the loop
if you choose str_replace method maybe str_ireplace would be better for case-insensitive results.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Eureka! Thanks, rturner! str_ireplace is just what I need for something I am working on. :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would probably stick to the preg_replace() alternative in this case, since it is backward compatible with php4.
Post Reply