Puting an array in preg_match!

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

    Puting an array in preg_match!

    Ok, I am trying to get it so if preg_match see's a word in the message from the array it will show a echo. This is what I have so far:

    [PHP]badwords = array("word1", "word2", "word3");
    $message = $_POST['message'];

    if(preg_match($ badwords, $comment))
    {
    echo"You posted a bad word!";
    }else{

    //do the rest!
    }[/PHP]

    Can someone sujest anyting?
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    Originally posted by Ajm113
    Ok, I am trying to get it so if preg_match see's a word in the message from the array it will show a echo. This is what I have so far:

    [PHP]badwords = array("word1", "word2", "word3");
    $message = $_POST['message'];

    if(preg_match($ badwords, $comment))
    {
    echo"You posted a bad word!";
    }else{

    //do the rest!
    }[/PHP]

    Can someone sujest anyting?
    preg_match doesn't allow arrays, try this:
    [PHP]
    $badwords = array("word1", "word2", "word3");
    $message = $_POST['message'];
    for($x = 0; $x < count($badwords ); $x++)
    {
    preg_match("/".$badwords[$x]."/", $message, $matches);
    if(count($match es) > 0)
    {
    echo"You posted a bad word!";
    }else{

    //do the rest!
    }
    }
    [/PHP]

    good luck

    Comment

    • Ajm113
      New Member
      • Jun 2007
      • 161

      #3
      I don't rarly get this error, but I get a "unexpected T_IF" on that if statment line.

      Comment

      • epots9
        Recognized Expert Top Contributor
        • May 2007
        • 1352

        #4
        Originally posted by Ajm113
        I don't rarly get this error, but I get a "unexpected T_IF" on that if statment line.
        thats cuz i forgot a semi-colon, but i updated the code above, re-copy it...i tested it and it worked

        Comment

        • Ajm113
          New Member
          • Jun 2007
          • 161

          #5
          Ok, now the script is repatedly posting a comment again and again! I have to praticly restart the table now!

          I don't know what I am doing wrong. But it seems that the "for" command mite have a bug.

          Here is the hole code:
          [php]
          $filout = array("word1"," word2", "word3");

          for($x = 0; $x < count($filout); $x++)
          {
          preg_match("/".$filout[$x]."/", $comment, $matches);
          if(count($match es) > 0)
          {
          echo'<div style="margin-left: 5cm; margin-top: 3cm;"><img src="lol.jpg" alt="Ha Ha!"><br>
          <font color="yellow" style="arial">H a Ha, Foo! You can not spam my site!</font>
          </div>';
          }else{
          $sql2="INSERT INTO comments(commen tTo, user, comment)VALUES( '$For', '$user', '$comment')";
          $result2=mysql_ query($sql2);
          header('Locatio n: index.php');
          }
          }[/PHP]

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            You put your insert inside the for loop, meaning the comment will be inserted once for every word you are trying to filter by.

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              Try this:

              [code=php]
              $filout = array("word1"," word2", "word3");
              $clean = true;
              foreach($fillou t as $badword)
              {
              preg_match("/".$badword. "/", $comment, $matches);
              if(count($match es) > 0)
              {
              echo'<div style="margin-left: 5cm; margin-top: 3cm;"><img src="lol.jpg" alt="Ha Ha!"><br><font color="yellow" style="arial">H a Ha, Foo! You can not spam my site!</font></div>';
              $clean = false;
              }
              }

              if($clean)
              {
              // Do insert
              }
              [/code]

              Comment

              • Ajm113
                New Member
                • Jun 2007
                • 161

                #8
                Thanks! I have been searching all over the web for this!

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Changed thread title: Removed superfluous 'I'm having trouble'.

                  Comment

                  Working...