Filtering out Bad Data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jtmphp
    New Member
    • Feb 2008
    • 9

    #1

    Filtering out Bad Data

    I have a site written in PHP that allows users to enter info into a form and upload an image. This information gets written to a MYSQL database. My question is how can I filter out dirty words and or pictures from being uploaded into my database ? Is there any simple way to handle this?
  • harshmaul
    Recognized Expert Contributor
    • Jul 2007
    • 490

    #2
    Hi,
    The images are going to have to be checked manually. (by yourself or a moderator), and the words can be filtered in a number of ways....

    try this one

    Code:
    <?php
    $swears = array("swear","bad","naughty");
    $strMessage = str_replace($swears, "***", $strMessage);
    ?>
    and then check this article...

    http://uk.php.net/str_replace

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      If you want multiple replacements:
      [php]
      <?php
      function filterContents( $_str)
      {
      $_badWords = array("badword1 ", "badword2", "badword3", "badword4") ;
      $_replace = array("****", "Removed", "OOPS!", "No can do!");

      for($_i = 0; $_i < count($_badWord s); ++$_i)
      {
      $_randKey = (rand()%count($ _replace)); // random key
      $_str = eregi_replace($ _badWords[$_i], $_replace[$_randKey], $_str); //replace with a random replacement.
      }
      return $_str; //return.
      }
      $_content = "string here.. maybe some submitted comment.. badword1 badword2 badword3..";
      echo filterContents( $_content);

      ?>
      [/php]

      Comment

      • harshmaul
        Recognized Expert Contributor
        • Jul 2007
        • 490

        #4
        markus, Love the replacements!

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by harshmaul
          markus, Love the replacements!
          You should've seen what i had for the badwords before i realised i better take them out if i wanna remain on thescripts ;)

          Comment

          • jtmphp
            New Member
            • Feb 2008
            • 9

            #6
            Perfect.
            Thanks!

            Comment

            Working...