SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jan 2001
    Location
    UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey All,

    I'm pretty new to PHP and mySQL and have had to try and learn it in 24 hours. I'm making a pen-pals database and everything works so far. There's only one problem...

    I need to know how I can filter out swear words from a submitted form field before it gets added to the database.

    So if someone entered "This site is crap" in a form, how would I change "crap" to a "*" for example? And I would like to be able to change a range of different swear words such as ****, ******* etc.... (Its a site for kids) :-)

    I've seen posts regarding changing HTML tags and email addresses, but nothing on swear words :-(

    Thank you SO much in advance! This bugger is really giving me a brain ache

    Cheers,

    Anthony
    It's what you do that counts!

  2. #2
    SitePoint Member
    Join Date
    Jan 2001
    Location
    UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please note that the *****'s above are supposed to be swear words, but sitepoint has obviously got a filter that I need!
    It's what you do that counts!

  3. #3
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should no that you can never filter everything - people will always be smarter than any scripts you right if they really want to say it.

    Its really very simple - just do a str_replace or eregi_replace on the submitted text. with str_replace you could just replace all the F's with **** but what about motherf? it would still be mother****. If you wanted the whole thing gone you could do it with eregi_replace you could get rid of the whole word with any of these other words in them.

    eregi_replace("[^ ]fword[^ ]", "*****", $text);

    would replace any word that has the fword as part of it.

    eregi_replace("fu[ck][kt ]", "*****", $text);

    Would replace any of the variable spellings of the word.

    oh and the ***** above are the way I typed them in...
    You get the idea. So use some str_replaces, some eregi_replaces if you want to, and thats all it is.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have never seen vb's code for doing this, but out of curiosity I wrote this little function that can do what you asked.
    function bad_words($str) {
    for($i=1;$i<=strlen($str);$i++) {
    $ast .= "*";
    }
    return $ast;
    }

    $bad_words = array("badword1", "badword3", "badword3");
    $str = "This is a joke badword1 badword2, badword3";
    foreach($bad_words as $bad) {
    $str = eregi_replace($bad, bad_words($bad), $str);
    }
    print $str;
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my attempt at making a simple word filter function:

    PHP Code:
    <?

    $foo 
    'I personally find you dumb and stinky.';
    echo 
    $foo filter($foo);

    function 
    filter($in)
    {
        
    $banned = array('dumb','stinky');

        foreach(
    $banned as $word)
        {
            for(
    $i=0;$i<strlen($word);$i++) $replace .= '*';
            
    $in eregi_replace($word,$replace,$in);
            unset(
    $replace);     
        }
        return 
    $in;
    }
    ?>
    output: I personally find you **** and ******.
    Last edited by jrap; Feb 21, 2001 at 15:35.

  6. #6
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're not so sweet-smelling yourself, RobP!
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  7. #7
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, but I make up for my awful odour by looking sexy in a mullet.


Bookmarks

Posting Permissions

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