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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts

    Very Smart Swear Censoring Script

    So, as a followup (just because I got bored) to a script I posted on here, I added on and added on, till I came with a pretty good script. I'd say it'll get rid of 90% of all attempts to cuss when a string is passed through this function, and many things are taken into account.

    PHP Code:
    <?php

    /**
     * @author Zangeel
     * @copyright 2009
     */


    function SmartCensor($string)
    {
        
    /**
         * Config Arrays
         **/
         
        //Words used to split swears, i.e. a.s.s
        
    $illegal = array("!""@""#""$""%""^""&""*""("")""-""_""+",
            
    "=""1""2""3""4""5""6""7""8""9""0"","".""/""\\""{",
            
    "}""[""]""~""`"">""<"";"":""'""\"""?""|");

        
    //Swears and their replacements
        
    $BadWords = array("a.ss""s.hit""f.uck""bitc.h""as.shole""fu.cker""sht""btch""f.uk""c.unt");
        
    $RePlace = array("***""*****""****""*****""*******""******""****""*****""****""****");
        
        
    //RegEx to find spaced out swears
        
    $RegEx = array(
        
    "(\s?+(a|A)\s?\s?+(s|S)\s?\s?+(s|S)\s+)" => " *** ",
        
    "(\s?+(s|S)\s?\s?+(h|H)\s?\s?+(i|I)\s?\s?+(t|T)\s?+)" => " **** ",
        
    "(\s?+(f|F)\s?\s?+(u|U)\s?\s?+(c|C)\s?\s?+(k|K)\s?+)" => " **** ",
        
    "(\s?+(b|B)\s?\s?+(i|I)\s?\s?+(t|T)\s?\s?+(c|C)\s?\s?+(h|H)\s?+)" => " ***** ",
        
    "(\s?+(a|A)\s?\s?+(s|S)\s?\s?+(s|S)\s?\s?+(h|H)\s?\s?+(o|O)\s?\s?+(l|L)\s?\s?+(e|E)\s?+)" => " ******* ",
        
    "(\s?+(f|F)\s?\s?+(u|U)\s?\s?+(c|C)\s?\s?+(k|K)\s?\s?+(e|E)\s?\s?+(r|R)\s?+)" => " ****** ",
        
    "(\s?+(s|S)\s?\s?+(h|H)\s?\s?+(t|T)\s?+)" => " **** ",
        
    "(\s?+(b|B)\s?\s?+(t|T)\s?\s?+(c|C)\s?\s?+(h|H)\s?+)" => " ***** ",
        
    "(\s?+(f|F)\s?\s?+(u|U)\s?\s?+(k|K)\s?+)" => " **** ",
        
    "(\s?+(c|C)\s?\s?+(u|U)\s?\s?+(n|N)\s?\s?+(t|T)\s?+)" => " **** "
        
    );
        
        
    /**
         *Start Process 
         **/
         
         //(1) Take care of spaced out swears via regex
          
    $string preg_replace(array_keys($RegEx), array_values($RegEx), $string);

         
    //EXPODE: Seperate the string word by word
         
    $ex explode(" "$string);

        
    //Alternate letters, e.g. @ for A, $ for S
        
    $alt = array("@""!""$""|""0");
        
    $real = array("a""i""s""i""o");
        
        
    //(2) Get rid of string seperators, check for swears
        
    for ($i 0$i sizeof($ex); $i++) {
            
    $x str_ireplace($illegal""$ex[$i]);
            if (
    in_array(strtolower($x), $BadWords)) {
                
    $ex[$i] = str_ireplace($BadWords$RePlace$x);
            }
        }
        
        
    //(3) Check for alternate spelling with special chars
        
    for ($i 0$i sizeof($ex); $i++) {
            
    $y str_ireplace($alt$real$ex[$i]);
            if (
    in_array(strtolower($y), $BadWords)) {
                
    $ex[$i] = str_ireplace($BadWords$RePlace$y);
            }
        }


        return 
    implode(" "$ex);
    }
    It uses three methods, the first, is finding spaced out swears using regex, my regex is sucky but I gave it a shot there, so basically a s s, or a ss or whatever is caught. The next method is stripping any possible non alphabetic chars, and checking if the alphabetic text contains a swear, if it does it replaces it with asterisks, the last is checking for alternative ways to spell, i.e. @$$ for a.s.s or sh!t, etc and so on.

    Here's an example of how it works

    PHP Code:
    echo SmartCensor("what are you doing a s s <br>"); 
    echo 
    SmartCensor("what are you doing @$$ <br>");
    echo 
    SmartCensor("what are you doing @s$ <br>");
    echo 
    SmartCensor("what are you doing as s <br>");
    echo 
    SmartCensor("what are you doing a.s.s.. <br>");
    echo 
    SmartCensor("hey stupid sh it go away  <br>");
    echo 
    SmartCensor("loser, f-u-c-k you <br>");
    echo 
    SmartCensor("assalamualaikum. <br>"); 
    Output:

    Code:
    what are you doing *** 
    what are you doing *** 
    what are you doing *** 
    what are you doing *** 
    what are you doing *** 
    hey stupid **** go away 
    loser, **** you 
    assalamualaikum
    All successful, and the last echo shows it wont censor the a word in the prefix of a word, because many languages have transliterations with "a-s-s" in the beginning, including english.

    Now it runs the string through two loop after each word is exploded. Now for a string like 2k words, this function may not be so efficient, but, its great for small guestbook type scripts, and itll bypass most popular attempts to bypass swears.

    There are holes in it of course it we nit pick it, but the average user will give up after trying to swear after the 10th attempt, and if he does bypass it he'd have to break up the word, an overkill to add in one swear

    I changed some of the words with periods in the badword array because this forum censors swears, just take the periods out
    Last edited by Zangeel; 08-01-2009 at 10:34 PM.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  2. Users who have thanked Zangeel for this post:

    jswany (08-04-2009)

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    In the two for-loops, you'll want to change <= to just <, or it will generate undefined index notices.

  • #3
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Oh yea, thanks I see what you mean! Edited*
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #4
    Banned
    Join Date
    Jun 2007
    Location
    Web Designer
    Posts
    321
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Can I integrate this script in my website for free ?

  • #5
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    No I charge 1,000,000 dollars
    Kidding, of course that's why I posted it up here, use it however you like!
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #6
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    cool script, but don't forget htmlspecialchars()

    eg. fùck - ****

    Edit: breaks on
    PHP Code:
    echo SmartCensor("loser, &[remove]#70;uck you <br>"); 
    just remove [remove]
    Last edited by funnymoney; 07-31-2009 at 04:45 PM.

  • #7
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Yeah, the alternate spelling array should take care of anything like that. There's unlimited words you could filter through so i just did the most common.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #8
    New Coder
    Join Date
    Jul 2009
    Location
    UK
    Posts
    89
    Thanks
    5
    Thanked 11 Times in 11 Posts
    i am using this script on my site. and ive found that if you use capitals with spaces it wont block it... well any capital letters for that matter. for example TOssER , T.O.S.S.E.R , T O S S E R. I have add a load of new words but cannot seem to stop the examples i have showed apart from that it is really good

  • #9
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Yep, know what the problem is? in_array is a case-sensitive function, so make sure all the swears in the badword array are LOWER-CASE, then change the two loops to this

    PHP Code:
        //(2) Get rid of string seperators, check for swears
        
    for ($i 0$i count($ex); $i++) {
            
    $x str_ireplace($illegal""$ex[$i]);
            if (
    in_array(strtolower($x), $BadWords)) {
                
    $ex[$i] = str_ireplace($BadWords$RePlace$x);
            }
        }
        
        
        
    //(3) Check for alternate spelling with special chars
        
    for ($i 0$i count($ex); $i++) {
            
    $y str_ireplace(strtolower($alt), $real$ex[$i]);
            if (
    in_array(strtolower($y), $BadWords)) {
                
    $ex[$i] = str_ireplace($BadWords$RePlace$y);
            }
        } 
    So before when someone wrote s.H.i.T it would strip the illegal chars and leave §HiT (§ = S) but in_array wouldn't recognize the word §hit with the word §HiT, so now we take what it leaves (i.e. §HiT) and make it all lowercase then check it. So just make sure the array has all lowercase swears and it'll work fine.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • Users who have thanked Zangeel for this post:

    jswany (08-04-2009)

  • #10
    New Coder
    Join Date
    Jul 2009
    Location
    UK
    Posts
    89
    Thanks
    5
    Thanked 11 Times in 11 Posts
    well that works well. the only one it dosnt work on now is the T O S S E R. then i saw your over post
    Quote Originally Posted by Zangeel View Post
    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
    so i tried adding
    PHP Code:
    $str implode(" "$ex);
    $str str_ireplace("T O S S E R""***"$str);
    // and so on then return str

    return $str
    and i tried adding it to this script but it dosn't work. although i think now im just going to far. In the end if they want to swear there going to work out a way. but cheers for the script works a dream except for the persistant users.

  • #11
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    anyone else see the funny side here? a post to prevent bad words which is bunged full of them

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #12
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Gotta love irony!

    @ Jswany: You looked at the wrong forum, that was a reply to another swear censor script look above your post, I posted a solution. Of course if it's a spaced swear, you want to get rid of, you need to use the regex I provided in the script, it's got a pattern to it so its easy to figure out.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #13
    New Coder
    Join Date
    Jul 2009
    Location
    UK
    Posts
    89
    Thanks
    5
    Thanked 11 Times in 11 Posts
    cheers zangeel.. your right wasn't hard to work out.. i was just being a bit thick lol.

  • #14
    New Coder
    Join Date
    Aug 2009
    Posts
    84
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Instead of using that monstrous Regular expression, use $string = strtolower(string); instead of making a capital alternative for each letter. :P It'll save a lot of space. :P

  • #15
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Heh heh, I had to learn basic regex to make this function so I know it sucks, but good idea, but I'm sure there's even easier ways to do it with regex.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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