Results 1 to 12 of 12

Thread: swear words

  1. #1
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413

    swear words

    Hello,

    did anybody already worked on something like a forum and he had to filter swear words?
    I am searching for the best solution to do so. So please any suggestions will be very much thanked.

    Thanks in advance

  2. #2
    Join Date
    Aug 2003
    Location
    California, USA
    Posts
    581
    I had this lying around

    PHP Code:
    <?php
    function badWordFilter($text)
    {
        
    $bad_word_list "****, ****, bitch, ***, whore, ****, ******, beaner, kike, damn";
        
    $bad_words explode(", ",$bad_word_list);
        foreach (
    $bad_words as $bad_word)
        {
            
    $filter '';
            for (
    $i 0$i strlen($bad_word); $i++)
                
    $filter .= '*';
            
    $text eregi_replace($bad_word$filter$text);
        }
        return 
    $text;
    }

    $str "This is a harassment test. **** *** ****.";
    echo 
    badWordFilter($str);
    ?>
    The only problem is that {harassment} shows up as {har***ment}. I'll see if I can find a solution to this.


    And as you can see the bad word filter in vBulletin is messing with my source code! :p

  3. #3
    Join Date
    Aug 2003
    Location
    California, USA
    Posts
    581
    Okay I have played with it a little bit.

    Luckily it is case insensitive (that might have been tricky otherwise).


    @$$ = *** ... had to change it in the source to make it legible because of VB's filter.

    PHP Code:
    <?php
    function badWordFilter($text)
    {
        
    $bad_word_list "****, ****, bitch, whore, ****, ******, beaner, kike, damn";
        
    $exact_words_list " @$$, @$$ "// note the spacing here.
        
    $exact_bad_words explode(", ",$exact_words_list);
        
    $bad_words explode(", ",$bad_word_list);

        foreach (
    $bad_words as $bad_word)
        {
            
    $filter '';
            for (
    $i 0$i strlen($bad_word); $i++)
            {
                
    $filter .= '*';
            }
            
    $text eregi_replace($bad_word$filter$text);
        }
        foreach (
    $exact_bad_words as $exact_bad_word)
        {
            
    $filter '';
            
    $split str_split($exact_bad_word);
            foreach (
    $split as $bad_char)
            {
                
    $filter .= ($bad_char == " ") ? " " "*";
            }
            
    $text eregi_replace($exact_bad_word$filter$text);
        }
        return 
    $text;
    }

    $str "This is a harassment test. @$$ . @$$.  .@$$.";
    echo 
    badWordFilter($str);
    ?>
    Problem is it doesn't recognize the last .***. (for obvious reasons).

    Maybe someone else has some input on how to do this? I'm probably going about it the wrong way.
    Last edited by etogre; 03-27-2008 at 01:56 PM.

  4. #4
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    thanks a lot for your answer the problem is not so much in the function the problem is somehow in the list of bad words. I need a good one to work upon... then the function it is as all php functions u write u repair and at last it will be good

  5. #5
    Join Date
    Apr 2000
    Location
    California
    Posts
    3,051
    To prevent partial words from being filtered out inside of other words, use word boundries.

  6. #6
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    ok maybe I wasn't so clear, I would like to have the list of swear words. The problem is not the function cause the function maybe will change with respect to each site, and the place of the swear words list... what I need is the list of swear words

    Thanks

  7. #7
    Join Date
    Aug 2003
    Location
    California, USA
    Posts
    581
    Well, now it's kinda off topic but others might be interested.

    Basically I modified phpBB's swear filter to allow dictionary without needing a database.

    PHP Code:
    <?php
    function badWordFilter($text)
    {
        
    // badword dictionary, works as "word" => "replacement",
        // allows for wildcards, for instance [email protected] with "[email protected]" will return har***
        
    $badwords = array( 
                    
    "***" => "a**"
                    
    "bitch" => "b*tch",
                    
    "****" => "c*nt",
                    
    "etc" => "etcetera",
        );
        
        
    $censors = array();
        foreach (
    $badwords as $word => $replacement)
        {
            
    $censors['match'][] = '#(?<!\w)(' str_replace('\*''\w*?'preg_quote($word'#')) . ')(?!\w)#i';
            
    $censors['replace'][] = $replacement;
        }
        if (
    sizeof($censors))
        {
            return 
    preg_replace($censors['match'], $censors['replace'], $text);
        }
        return 
    $text;
    }

    $str "This is a harassment test. ***. ***.  .***. etc"
    echo 
    badWordFilter($str); // This is a harassment test. ***. ***. .***. etcetera
    ?>

  8. #8
    Join Date
    Apr 2000
    Location
    California
    Posts
    3,051
    I see, well you might ask people to email you their list, because I don't think you'll be able to have people post the words you need here (they'd be filtered).

  9. #9
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    A simple Google "swear word list" returns this first great site:

    http://www.noswearing.com/

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  10. #10
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    Quote Originally Posted by azizny View Post
    A simple Google "swear word list" returns this first great site:

    http://www.noswearing.com/

    Peace,
    Thanks I have already seen it I contacted them to see if I can get the list, what I got is a mail delivery failure

  11. #11
    Join Date
    Aug 2007
    Location
    Greece
    Posts
    390
    here you are
    NOT a webhost!helping here just for the fun of it!
    G(r)eek inside.

  12. #12
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    thanks very much tix3 guess I can start with that then if any word missed I'll add it

Posting Permissions

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