Badwords Checker In PHP Forms

Discussion in 'PHP' started by Bohra, Oct 14, 2009.

  1. #1
    Well i am trying to add a badwords checker in a form where if there is a badword entered a error is displayed when submit

    do you know of any function which would do this ??
     
    Bohra, Oct 14, 2009 IP
  2. kind_of_the_cash

    kind_of_the_cash Active Member

    Messages:
    852
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    85
    #2
    kind_of_the_cash, Oct 14, 2009 IP
  3. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #3
    
    <?php
    
    $badwords = array('badword1','badword2','badword3','badword4');
    
    $badwords = explode(",", $badwords);
    
    foreach($badwords as $badword) {
    preg_replace("/$badword/i", "FIXED WORD", $badword);
    }
    
    ?>
    PHP:
    The preg $badword can be replaced with say $text, so that it doesn't just fix the array, but it fixes a paragraph. Example:

    
    <?php
    
    $text = "hello mother badword1s!! i've been on t3h badword1ing interwebs for some time now. now go eat badword2 and lets jump around like a bunch of badword3holes and be badword4s!!!!";
    
    $badwords = array('badword1','badword2','badword3','badword4');
    
    $badwords = explode(",", $badwords);
    
    foreach($badwords as $badword) {
    preg_replace("/$badword/i", "*****", $text);
    }
    
    echo $text;
    
    ?>
    PHP:
    This would output...

    Hope that helps :D
     
    almondj, Oct 15, 2009 IP
  4. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #4
    is it possible like if there is a badword it will tell that its false and give a error .. i mean the function
     
    Bohra, Oct 15, 2009 IP
  5. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #5
    What do you mean? The function will only replace words that you define as bad ;).
     
    almondj, Oct 15, 2009 IP
  6. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #6
    no what i want is like a form checker like its a email submission people put badwords in thier emails all i want is that if there is a badword the function returns false
     
    Bohra, Oct 15, 2009 IP
  7. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #7
    may be following function can be helpful..

    
    function contains_badword($str_to_check)
    	{
    		$found = false;
    		$bad_words = array('badword1', 'badword2', 'badword3', 'badword4');
    		foreach($bad_words as $bad_word)
    		{
    			$found = preg_match("/$bad_word/i", $str_to_check);
    			if($found > 0)
    				break;
    		}
    		if($found > 0)
    			return true;
    		else
    			return false;
    	}
    
    PHP:
     
    Last edited: Oct 16, 2009
    mastermunj, Oct 16, 2009 IP