Checking for bad words in a posted comment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajm113
    New Member
    • Jun 2007
    • 161

    #1

    Checking for bad words in a posted comment

    -Oops, sorry!- I posted in the wrong area! Can someone move this thread please?

    Ok, I got a array going with the words I want. So if someone posted any of those words I want the script to show a message. So this is what I have so far:

    [PHP]$message = $_POST['message'];

    $filter = array("word", "word2", "word3");

    if($message == badword)
    {

    //Show error message

    }else{
    //post message

    }[/PHP]

    I know the if line wont work, but I just wanted to throw it out their as a example of what I want to do.

    Any suggestions?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    I'm moving this thread to the PHP Forums.
    Please refrain from posting in the Articles section!

    Moderator

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      You could do something like this:
      [code=php]
      $message = $_POST['message'];
      $searchWord = array("word1", "word2", "...");

      foreach($search Word as $word)
      {
      if(strpos($mess age, $word) != false) {
      echo "Invalid word detected!";
      exit;
      }
      }
      echo "Its all good!";
      [/code]

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Ajm.

        [code=php]
        $badwords = array( ... );
        $post = implode(' ', array_diff(expl ode(' ', $post), $badwords));
        [/code]

        Comment

        • Jankie
          New Member
          • May 2007
          • 58

          #5
          Heya Pbmods

          Would you please mind of you can afford to elaborate a bit on this?

          Code:( php )

          1. $badwords = array( ... );
          2. $post = implode(' ', array_diff(expl ode(' ', $post), $badwords));

          Does this just filter bad words silently or how this would fit in the code above?

          Thanks a lot

          Comment

          • luke14free
            New Member
            • Apr 2007
            • 79

            #6
            This is the answer i think...
            [PHP]
            $string=$_POST['string'];
            $bad=array();
            foreach($bad as $i){
            $string=str_rep lace($i," CENSORED ",$string);
            }
            [/PHP]
            Have a nice day,
            Luke14free

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Jankie.
              Originally posted by Jankie
              Would you please mind of you can afford to elaborate a bit on this?

              [code=php]$badwords = array( ... );
              $post = implode(' ', array_diff(expl ode(' ', $post), $badwords));[/code]

              Does this just filter bad words silently or how this would fit in the code above
              This code does filter out the bad words silently. It explode()s the post, using space characters (i.e., creates an array containing every word in the post). Then it removes all words that match any of the *keys* in $badwords (you'll need to make the bad words keys), and then implode()s the array back into a string, reinserting the spaces.

              The one problem with this algorithm is that it doesn't take punctuation into account.

              Comment

              • kovik
                Recognized Expert Top Contributor
                • Jun 2007
                • 1044

                #8
                The foreach method would be your best bet, but don't use strpos(). strpos() can return 0, which is equivalent to false. Use stristr(), which will either return the string (if you which to str_replace it) or FALSE, and is case insensitive as well.

                pbmods method could be done much faster by using only str_replace() instead of implode and explode.

                Comment

                Working...