Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Filter Text from the form using php

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default Filter Text from the form using php

    I have this suggestion form but I don't want people to swear and stuff cuz I put up one last night and I got 350 bad words, links, advertisements, and a whole bunch of stuff...
    But anyways I don't know how to filter through words and to see if a variable is too long using strings...
    I have the code below of the page...
    Code:
    <html>
    <head>
    <style type="text/css">
    html,body{
    	font-family: Trebuchet Ms;
    	font-size: 12px;
    }
    .input{
    	width: 200px;
    	border: 1px solid #4e4e4e;
    	padding-left: 3px;
    	font-family: Trebuchet Ms;
    	font-size: 16px;
    	font-weight: bold;
    	background-color: #FFFFFF;
    }
    .textarea{
    	width: 350px;
    	height: 100px;
    	border: 1px solid #4e4e4e;
    	padding-left: 3px;
    	font-family: Trebuchet Ms;
    	font-size: 13px;
    }
    </style>
    </head>
    <body>
    <form action="193.php" method="post" />
    <br />Your Display Name:
    <br /><input type="text" name="1" class="input"/>
    <br />
    <br />Your Message:
    <br /><textarea class="textarea" name="2"></textarea>
    <br />
    <br /><input type="submit" class="input" />
    </form>
    <?php
    $dName = $_POST["1"];
    $Message = $_POST["2"];
    ?>
    If you explain what to put where and how to edit it pretty well then I can do it... or if you feel that you could just write the code yourself faster than doing that it's fine, it's your choice...

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Well, you need to think about specifically step by step how to alter the text.
    A simple bad word filter is incredibly simple... just replace [word] with [] or [****], etc.

    $string = str_replace('findword','replaceword_orblank',$string);

    Efficiently, you could use an array-- array('word1','word2','word3') -- and place the replace within a foreach loop, removing each word.

    You may also want to consider placement-- hello contains hell; you may want to check that there is a space or other punctuation on either side of the word.

    Replacing links wouldn't be all that hard--
    You could use regex, or you could use a less efficient/simpler (but more complex statements) method with substr, etc.
    PHP Code:
    while (strpos('http://',$string)!==FALSE) {
    $string substr($string,0,strpos($string,'http://')).substr($string,strpos($string,' ',strpos($string,'http://'));

    (Can't promise that'll work, but the concept is there-- and you should consider if that's exactly what you want anyway.)


    And you can do some more, as well, though nothing you will do can get around human creativity, so you'll need to manually check the posts... yep. Ban those who post offensive comments if possible, as well.

    A simple captcha or even just a question "what color is grass? [blue] [red] [green]" would also help with bots.
    Your security doesn't need to be perfect if there isn't a real threat-- just some spam, and it is likely that the bots will give up rather than adapt as your site probably is not any particular benefit to them... they just search out forms and post.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    K, thanks man I'll try it out... I'll try the string out and I don't feel comfortable with it I'll try the array out... thanks...

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If you want a lot of words, the array is absolutely the best method. For just a few, lines of code would work.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Efficiently, you could use an array-- array('word1','word2','word3') -- and place the replace within a foreach loop, removing each word.
    No need to loop, str_replace() takes arrays:
    Code:
    str_replace(array('monkeys', 'fish'), array('*******', '****'), $str);
    Last edited by Twey; 10-18-2007 at 10:26 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah. Very nice. I'd say don't use a second array, then; just use an empty string or '****', which would be the same value for every element of the other array.
    I don't see the point in having a separate replace value for each item, though perhaps you'd like the right number of asterisks, though that would make the original word more clear.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    or you could just put the ole *beep*


    although its always funny to replace with random words to confuse the reader haha

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Better yet, insert random foreign language words. It will seem like it has some meaning to the average reader, but actually be complete nonsense.

    I like the 2-year-old approach of "[CORRECTION] I said a no no word [/CORRECTION]" as well.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A "no no word?" xD
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    - Josh

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
  •