Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    75
    Thanks
    1
    Thanked 12 Times in 12 Posts

    PHP swearing smart censor

    I made a script for my website to censor swear words from being posted and decided to share. What's special about this function is that it will even catch most attempts to bypass it, like badwo;r;d and cra.p:
    PHP Code:
    //Function written by Blueblib, blueblib@gmail.com
    //Do whatever you want with it just please leave credit where credit is due.
    function censor($bb_content) {
        
    $bb_profanity_delimiter_array = array(" "";"":""."",""/""-""_");
        
    $bb_profanity_array = array("badword""crap");
        
    $bb_clean_array = array("fiddle stick""sugared blueberry""kawabunga""apple pie""fairy pamper");
        
    shuffle($bb_profanity_array);
        
    shuffle($bb_clean_array);
        
    $bb_profanity_destroy_array = array();
        foreach (
    $bb_profanity_array as $bb_profanity) {
                if (
    array_search($bb_profanity$bb_profanity_destroy_array)===FALSE) {
                    
    $bb_profanity_destroy_array[] = $bb_profanity;
                }
            foreach (
    $bb_profanity_delimiter_array as $bb_profanity_delimiter) {
                
    $bb_profanity_length strlen($bb_profanity);
                
    $bb_profanity_unique = array();
                
    $bb_profanity_counter 0;
                while (
    $bb_profanity_counter<$bb_profanity_length) {
                    
    $bb_profanity_unique[] = $bb_profanity[$bb_profanity_counter];
                    
    $bb_profanity_counter++;
                }
                
    $bb_profanity_counter_minimum = -2;
                while (
    $bb_profanity_counter_minimum<=$bb_profanity_length) {
                
    $bb_profanity_counter_sub 0;
                while (
    $bb_profanity_counter_sub<=$bb_profanity_length) {
                
    $bb_profanity_compiled "";
                
    $bb_profanity_counter_sub_sub 0;
                foreach (
    $bb_profanity_unique as $bb_profanity_part) {
                    
    $bb_profanity_compiled .= $bb_profanity_part;
                    if (
    $bb_profanity_counter_sub_sub<($bb_profanity_counter_sub-1)&&$bb_profanity_counter_sub_sub>$bb_profanity_counter_minimum) {
                        
    $bb_profanity_compiled .= $bb_profanity_delimiter;
                    }
                
    $bb_profanity_counter_sub_sub++;
                }
                if (
    array_search($bb_profanity_compiled$bb_profanity_destroy_array)===FALSE) {
                    
    $bb_profanity_destroy_array[] = $bb_profanity_compiled;
                }
                
    $bb_profanity_counter_sub++;
                }
                
    $bb_profanity_counter_minimum++;
                }
            }
        }
        foreach (
    $bb_profanity_destroy_array as $bb_profanity_destroy) {
        
    $bb_content str_ireplace($bb_profanity_destroy$bb_clean_array[rand(0count($bb_clean_array)-1)], $bb_content);
        }
        return 
    $bb_content;
        }
        echo 
    censor("this is a crap comment, it's extremely hard to bypass this cra.p");// I got "this is a sugared blueberry comment, it's extremely hard to bypass this fairy pamper" 
    Feedback/Improvements much appreciated. I decided that looking for multiple seperators like ba.dwor;d would be too CPU intensive.

  • #2
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    I took your concept and made my own smart censor

    PHP Code:
    function SmartCensor($string)
    {
        
    $illegal = array(";""-"".""_""^");
        
    $BadWords = array("***""*****""***");
        
    $RePlace = array("***""*****""***");
        
    $ex explode(" "$string);

        for (
    $i 0$i <= count($ex); $i++) {
            
    $x str_ireplace($illegal""$ex[$i]);
            if (
    in_array($x$BadWords)) {
                
    $ex[$i] = str_ireplace($BadWords$RePlace$x);
            }
        }
        return 
    implode(" "$ex);
    }

    echo 
    SmartCensor("So one day I_ was wa_l_k;ing down the street b________it;ch"); 
    Simple and straight forward. Turns the string into an array to separate words, then strips the illegal characters like _ ; . etc, if the cleaned up word is in the bad word array, then it censors it, so it will look for multiple separators, and it'll only strip evil chars from bad words.

    The one problem with it I see is a bad word like "a s s" wont be censored.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #3
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Haha LOL, the forum censored the words in the badword array, sorry, but you get the idea.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #4
    New Coder
    Join Date
    May 2009
    Posts
    75
    Thanks
    1
    Thanked 12 Times in 12 Posts
    Great work your version is much more effecient.
    Quote Originally Posted by Zangeel View Post
    The one problem with it I see is a bad word like "a s s" wont be censored.
    Can't you add " " to $illegal, like I did? Or would it not work because of this:
    PHP Code:
    $ex explode(" "$string); 

  • #5
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Well because I separated words based on spaces, if they wrote something like "a s s" in the array generated by explode() it would look like

    PHP Code:
    array("a","s","s"); 
    thus making each letter a word of its own. The only solution I would think is taking the final string and manually checking for words within it like adding to the function

    PHP Code:
    $str implode(" "$ex);
    $str str_ireplace("a s s""***"$str);
    // and so on then return str

    return $str
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •