Strpos and a banlist of bad words

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
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Strpos and a banlist of bad words

Post by Citizen »

I'm trying to make a banlist of words for a form.

Code: Select all

$findme1 = 'badword';
		$pos1 = strpos($content, $findme1);
                 if ($pos2 !== false) {
			echo "That word is not allowed.";
Is there a way to add multiple words instead of just one or do I have to repeat this code over and over again for each banned word?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Is there a way to add multiple words instead of just one or do I have to repeat this code over and over again for each banned word?
No you don't, yes there is.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Any help as to how? I've been looking all over php.net and havent found anything on multiple entries.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it involves array functions.

http://php.net/ref.array
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

OK, I'll help out a bit more.

Code: Select all

$black = array('rectum', 'brother lucker', 'choad', 'pentration', 'microsoft');
foreach ($black as $test) {
    if (strpos($content, $test)) {
        echo 'Bad word, bad child';
        break;
    }
}
Err this does the same and is probably better, I know feyd would prefer this anyway ;)

Code: Select all

$black = as above;
if (in_array($test, $black)) {
    echo 'Bad word, bad child';
}
Post Reply