Badword Array Replacement [SOLVED]

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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Badword Array Replacement [SOLVED]

Post by tecktalkcm0391 »

Can someone help me with this. I want the following PHP code to look at the variable $username_bad_check which is equal to the $username (in this case 'ass12345'). If it finds any of the words in the $badwords array. Then it displays the message.

Code: Select all

<?php

$username = 'ass12345';
$username_bad_check = $username;
$username_bad_check = strtolower($username_bad_check);
$username_bad_check = " ".$username_bad_check." ";
    
// Array, Badword, Goodword
// Goodword is not used.
$badwords = array();

    $badwords[] = array('ass', ' * ');

if($username_bad_check == $badwords){
	echo "OH NO YOU DIDN\'T. We have found a bad word in your username\! You may have put something bad in by mistake, but remember if you use bad words\* on our site.<br><br><Br>*The term \"Bad Word\" refeers to a curse words, an explicit words, or any other inappropriate word, or word we find to be inappropriate.";
    }

?>
Last edited by tecktalkcm0391 on Tue Jun 06, 2006 10:02 pm, edited 1 time in total.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

Personally I'd use a foreach loop to check and see if any of the words in your bad words array are found in the name itself.
For instance...

Code: Select all

foreach ($badwords as $word)
{
      if (strstr($username, $word))
          echo "Your username cannot contain the word " . $word;
}
You get the idea. Basically you go through the entire array and see if any of those words are contained in the user name.
Hope this helps,

Jade
Post Reply