Bad words filter, exit instead of replace

Discussion in 'PHP' started by sw2geeks, Oct 8, 2008.

  1. #1
    Hi, I have been working some in flash and am just starting to mess around with php. I have been able to find a lot of sample code on how to replace bad words, but I am having trouble finding something showing how to exit or stop the script before it post when a bad word is found. I already have a $badwords list set up, so basically how do you write in php to look though the $text field and exit the script before it sends the $text to the database? Thanks for any help or a better place to post this.
     
    sw2geeks, Oct 8, 2008 IP
  2. Limotek

    Limotek Peon

    Messages:
    165
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Have a look at the following:

    in_array - checks if a string is in an array
    strstr - checks if string B is in string A
    strtolower - converts all characters to lower case (makes it easier to match them up)

    You should be able to use a combination of the above to do what you want. There are a lot of good examples toward the bottom of each page as well.
     
    Limotek, Oct 8, 2008 IP
  3. sw2geeks

    sw2geeks Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the links Limotek,

    After looking at the array link I am still having trouble figuring out how to exit the php when the function returns true. Here is one of the examples from the link you sent me.

    <?
    function deep_in_array($value, $array) {
        foreach($array as $item) {
            if(!is_array($item)) {
                if ($item == $value) return true;
                else continue;
            }
           
            if(in_array($value, $item)) return true;
            else if(deep_in_array($value, $item)) return true;
        }
        return false;
    }
    ?>
    
    Code (markup):
    I think I should exit with something like this, but I am not sure what to define as the condition.

    if($condition == true) exit;
    Code (markup):

    Right now I am using this code here that is filtering/replacing out the bad words. It seems to work good, but I would prefer to just exit out to keep the post from being sent.

    $bad_words = explode('|', 'badword1|badword2|badword3');
    foreach ($bad_words as $naughty)
    {
    $name = eregi_replace($naughty, "###", $name);
    }
    
    Code (markup):
    I am basically trying to limit the bad words in the names kept in a flash games high score database.

    Thanks
     
    sw2geeks, Oct 8, 2008 IP
  4. classic

    classic Peon

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You don't need to exit from PHP script its bad practice, instead detect the bad word and do
    1. Send message to user that violent language bla bla and don't update database or
    2. Replace bad words width ?#?@ so that text doesn't loose sense eg
    "You fucking moron !!!!"
    "You #@$@ moron !!!!"

    here is PHP code for #2
    
    $bad_words = explode('|', 'fucking|fuck|fucker');
    $replace = '#@!@?';
    $text = "You FuCking  moron !!!!";
    
    $goodtext = str_ireplace($bad_words,$replace,$text);
    var_dump($goodtext); //"You #@!@?  moron !!!!"
    
    //or you can do better replace with replacemenet for each bad word
    $bad_words = array('fucking'=>'Pretty' , 'fuck'=>'Nice' , 'fucker'=>'Sweetheart');
    
    $goodtext = str_ireplace(array_keys($bad_words),array_values($bad_words),$text);
    var_dump($goodtext); //"You Pretty  moron !!!!"
    
    
    PHP:
     
    classic, Oct 8, 2008 IP