SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,027
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    What is the best way to filter contents of a form against bad words?

    Hello all,

    What is the best way to filter a set of words from a document?
    To be exact say i have a list of words like:
    xy, zx, xx, yx

    and I want to filter a document submitted via a form, that is via Textarea of the form, against these words so that should these words occur then they are to be replaced by ***.

    What is the best and most efficient way of doing this? So to be exact before storing the content of a form into the MySQL DB I want to filter the content against these words.

    Best Regards,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Zealot Ripe's Avatar
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    str_replace
    (array("xy""zx""xx""yx"), "***"$_POST["textarea"]);
    ?>
    Should work.

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,027
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Than you Ripe, your suggestion works superbly.

    One more question: what is the best way to store multiple values under a field in MySQL? I mean in case of words to be filtered, each of our Blog owners (users) can have their own set of words to filter. So what is the best way to store multiple words for each user under the field filter for that user?

    Best regards,
    And thanx again for your quick suggestion


    Quote Originally Posted by Ripe View Post
    PHP Code:
    <?
    str_replace
    (array("xy""zx""xx""yx"), "***"$_POST["textarea"]);
    ?>
    Should work.

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Using str_replace will not work.

    PHP Code:
    <?php

    function censor $m )
    {
        
    $len strlen$m[1] );
        return 
    str_repeat'*'$len );
    }

    $words = array( 'bad''nono''ummmno' );
    $str 'I like to say bad bar badwords yes i do you nono and ummmno D:';
    $str preg_replace_callback'/\b((?:' join$words'|' ) . ')\S*)\b/''censor'$str );

    var_dump$str );
    Outputs: I like to say *** bar ******** yes i do you **** and ****** D:

    For storing bad word in a database...I say one word one row.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,027
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    The prior suggestion worked superbly.
    Thanks anyway.


    Quote Originally Posted by logic_earth View Post
    Using str_replace will not work.
    PHP Code:
    <?php

    function censor $m )
    {
        
    $len strlen$m[1] );
        return 
    str_repeat'*'$len );
    }

    $words = array( 'bad''nono''ummmno' );
    $str 'I like to say bad bar badwords yes i do you nono and ummmno D:';
    $str preg_replace_callback'/\b((?:' join$words'|' ) . ')\S*)\b/''censor'$str );

    var_dump$str );
    Outputs: I like to say *** bar ******** yes i do you **** and ****** D:

    For storing bad word in a database...I say one word one row.

    Anoox search engine volunteer

    www.anoox.com

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Make sure you show a confirmation page to these abusive users that suggests their bad words have been accepted ( though behind the scenes of course you *** them out as you suggest ).

    Otherwise you will have legions of the little b4st4rds making up f00kin swearwords to get round your filter.

    So let them think it worked first time... and reduce your headaches.

  7. #7
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,027
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ThanX

    Quote Originally Posted by Cups View Post
    Make sure you show a confirmation page to these abusive users that suggests their bad words have been accepted ( though behind the scenes of course you *** them out as you suggest ).

    Otherwise you will have legions of the little b4st4rds making up f00kin swearwords to get round your filter.

    So let them think it worked first time... and reduce your headaches.

    Anoox search engine volunteer

    www.anoox.com


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
  •