1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Function to Check Bad Words

Discussion in 'General Programming Chat' started by s-c-0-r-p-i-a-n, Aug 28, 2011.

  1. s-c-0-r-p-i-a-n

    s-c-0-r-p-i-a-n BANNED BANNED

    Joined:
    Nov 20, 2010
    Messages:
    37
    Likes Received:
    2
    Code:
    <?php
    function isBadWord($text)
    {
    
    //First we list the bad words in array
    $badwords = array(
    'truck',
    'hi',
    'lie',
    'bad',
    'night',
    'eg'
    );
    
    //Then we perform the bad word check
    foreach($badwords as $badwords)
    {
    if(stristr($text,$badwords))
    {
    return true;
    }
    }
    return false;
    }
    ?>
    First we need to list the bad words in an array. Then we perform the check using the stristr function which searches for the bad words inside the given text.

    If no bad word is found it returns FALSE.

    Here is an example of how to use this function

    Code:
    <?php
    
    $text = '';� //Put your text here to check
    
    if(isBadWord($text))
    {
    echo 'Bad Word Found!';
    }
    else
    {
    echo 'No Bad Word!';
    }
    
    ?>
     
  2. Senotaru

    Senotaru Registered Member

    Joined:
    Jan 17, 2011
    Messages:
    67
    Likes Received:
    11
  3. Crazy

    Crazy Jr. Executive VIP

    Joined:
    Jun 13, 2009
    Messages:
    640
    Likes Received:
    319
    Occupation:
    VB, C#, XHTML, CSS, PHP, MySQL, JavaScript, jQuery
    Location:
    Everywhere
    He's not checking the array, he's checking text. The array is his "bad word" list."
     
  4. xenon2010

    xenon2010 Regular Member

    Joined:
    Apr 27, 2010
    Messages:
    231
    Likes Received:
    48
    Occupation:
    web and desktop apps programmer
    Location:
    prison
    Home Page:
    you don't need to do the loop...
    you can search for any word inside the array directly.
    its way faster than the looping..
     
  5. Narrator

    Narrator Power Member

    Joined:
    Oct 5, 2010
    Messages:
    507
    Likes Received:
    396
    Occupation:
    Internet Marketing
    Location:
    /dev/null
    Another way to do it
    PHP:
    $badWords=array("badword","anotherbadword");
    $replacements=array("goodword","anothergoodword");
    $fixed=str_ireplace($badWords,$replacements,$string);
     
  6. xenon2010

    xenon2010 Regular Member

    Joined:
    Apr 27, 2010
    Messages:
    231
    Likes Received:
    48
    Occupation:
    web and desktop apps programmer
    Location:
    prison
    Home Page:
    neat solution :cool:
     
  7. GrizzlyAdams

    GrizzlyAdams Regular Member

    Joined:
    Feb 17, 2011
    Messages:
    479
    Likes Received:
    405
    Occupation:
    Freelancer
    Location:
    British Columbia
    This type of script is also good for turning keywords into interlinks like wikipedia does.

    I use it on all my static sites.

    Damn I love PHP :)
     
  8. xenon2010

    xenon2010 Regular Member

    Joined:
    Apr 27, 2010
    Messages:
    231
    Likes Received:
    48
    Occupation:
    web and desktop apps programmer
    Location:
    prison
    Home Page:
    PHP was my first love :D
    I've worked with asp.net and php before.. but damn man php is way way better for developing web apps..
     
  9. akiaki

    akiaki Registered Member

    Joined:
    Aug 8, 2011
    Messages:
    87
    Likes Received:
    10
    What do you mean by this? Either I've lost you or you've misunderstood the OP. She's iterating through a list of words and testing a text body to see if each one might be in there. Beside the str_replace() solution (preg_* could be used the same), how do you suggest doing it without a loop?

    And speaking of preg_*, if you use a regular expression approach (which, mind you, is computationally more expensive, but usually not enough to notice), you can get better at finding variations that users use to get around the list of bad words. You can see if bad words are included inside of others, used on their own, had letters replaced with numbers, had punctuation or spaces inserted in them, and lots more.

    Moreover, this kind of simple filtering may get some of the easy stuff, but if you really want to get the hard stuff, I'd suggest hitting up the PHP manual for the functions similar_text, metaphone, levenshtein and soundex.

    Last, I just want to throw in my opinion that censoring free speech sux. ;)