Search string for words in array?

Discussion in 'PHP' started by LazyD, Dec 16, 2006.

  1. #1
    So I have a contact page on my site with a simple form with the following fields:

    Name
    Email
    Comments

    Just for users to submit errors on the site, etc. Lately I have been getting tons of Viagra, Vicodin, replica watch kind of stuff in my mailbox from the form. I wanted to make a list of banned words in an array that I could easily add to at any time and then have the code iterate through the array evaluating the "Name" input to see if any of the POSTed strings had any of the banned words in them.

    I also wanted to set it up so I could I easily use it in an already existing IF statement by adding an ELSEIF like the following:

    elseif (banned word exists) {
    (this would be blank or have a message saying it was rejected)
    }

    Can anyone please help me, ive been messing around with different techniques for hours and the form info is still running right through.
     
    LazyD, Dec 16, 2006 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?
    $banned = array
    (
    "penis-pump",
    "viagra",
    "some other disgusting product only to be used by the weak and desperate"
    );
    if($_POST)
    {
    	foreach($_POST as $key => $value)
    	{
    		foreach($banned as $x)
    		{
    			if(preg_match("/$x/si", $value))
    			{
    				die("error message here");
    			}
    		}
    	}
    }
    ?>
    
    PHP:
    EDIT : I made a mistake, copy it now
     
    krakjoe, Dec 16, 2006 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Barti1987, Dec 16, 2006 IP