rmelino Posted March 4, 2011 Share Posted March 4, 2011 Hello, I'm having trouble getting a piece of code to work and as a result right now i have bloated code on a simple task. Currently my code looks like this: //convert responses into one string to check for spam $full_string = $name.' | '.$email.' | '.$message; //look for spam words if ( (stristr($full_string, 'test')) || (stristr($full_string, 'http')) || (stristr($full_string, 'www')) || (stristr($full_string, 'site')) || (stristr($full_string, 'fake')) || (stristr($full_string, 'viagra')) || (stristr($full_string, 'cialis')) || (stristr($full_string, 'asdf')) || (stristr($full_string, 'txt')) || (stristr($full_string, 'doc')) || (stristr($full_string, 'xls')) || (stristr($full_string, 'htm')) || (stristr($full_string, 'sale')) || (stristr($full_string, 'free')) ){ I would like to instead place all my 'spam' words in an array like this $spam_array = array('test', 'viagra', cialis...etc If i had all my spam words in an array, how would i write an if statement to check if any of those words appeared in $full_string ? Thanks for your help! Quote Link to comment Share on other sites More sharing options...
flolam Posted March 4, 2011 Share Posted March 4, 2011 $spam = array("test", "viagra"); foreach ($spam as $spamword) { if (strrpos($full_string, $spamword)) { echo "asdf"; } } Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 You could also use in_array(): http://php.net/manual/en/function.in-array.php Quote Link to comment Share on other sites More sharing options...
flolam Posted March 4, 2011 Share Posted March 4, 2011 But in_array() wouldn't work if the string was for example this: $full_string = "footestfoo; Quote Link to comment Share on other sites More sharing options...
rmelino Posted March 4, 2011 Author Share Posted March 4, 2011 thanks for your responses. I tried using in_array(): however i don't know how to use that in conjunction with stristr $spam = array("test", "viagra"); if (in_array(stristr($full_string), $spam)) { echo "This is SPAM";} What am i doing wrong in the example above? Quote Link to comment Share on other sites More sharing options...
flolam Posted March 4, 2011 Share Posted March 4, 2011 Checking the string for array items is easier than checking the array for string parts, so I recommend to use the solution I posted above. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 4, 2011 Share Posted March 4, 2011 I agree with flolam; I should have looked at his post a little closer. It looked like something that in_array() could help with, but that isn't the case this time. Sorry about the confusion. Quote Link to comment Share on other sites More sharing options...
rmelino Posted March 4, 2011 Author Share Posted March 4, 2011 Flolam, I tried your solution but keep getting Parse error: syntax error, unexpected T_ELSE ... i think because i have multiple if statements within yours? Here is a fuller version of the code: $full_string = $name.' | '.$email.' | '.$message; $spam = array("test", "viagra"); foreach ($spam as $spamword) { if (strrpos($full_string, $spamword)) { //if flagged for spam, insert into temp db $query = @mysql_query("INSERT INTO lsome_db () VALUES ()"); /***********START SPAM ALERT EMAIL*******************/ include $_SERVER['DOCUMENT_ROOT'] . "/email_templates/email_spam_alert.php"; //lead control $echo = '<h2>SPAM ALERT:<br /><br /></h2><p>'.$email_err.'<br/>'.$subject_err.'<br />'.$message_err.'</p>'; if ($lead_control == 1) { mail($email_err, $subject_err, $message_err, $headers_err); } elseif ($lead_control == 2) { echo $echo; } elseif ($lead_control == 3) { mail("email@email.com", $subject_err, $message_err, $headers_err); } if ($lead_control == 2) { echo '<h1>ECHO REPORT</h1>'; } else { header("Location: /contact.html?id=$username"); } /***********END SPAM ALERT EMAIL*******************/ } } else { //send off a different email any ideas? Quote Link to comment Share on other sites More sharing options...
flolam Posted March 4, 2011 Share Posted March 4, 2011 the last else should be before the last } (which closes the foreach loop): $full_string = $name.' | '.$email.' | '.$message; $spam = array("test", "viagra"); foreach ($spam as $spamword) { if (strrpos($full_string, $spamword)) { //if flagged for spam, insert into temp db $query = @mysql_query("INSERT INTO lsome_db () VALUES ()"); /***********START SPAM ALERT EMAIL*******************/ include $_SERVER['DOCUMENT_ROOT'] . "/email_templates/email_spam_alert.php"; //lead control $echo = '<h2>SPAM ALERT:<br /><br /></h2><p>'.$email_err.'<br/>'.$subject_err.'<br />'.$message_err.'</p>'; if ($lead_control == 1) { mail($email_err, $subject_err, $message_err, $headers_err); } elseif ($lead_control == 2) { echo $echo; } elseif ($lead_control == 3) { mail("email@email.com", $subject_err, $message_err, $headers_err); } if ($lead_control == 2) { echo '<h1>ECHO REPORT</h1>'; } else { header("Location: /contact.html?id=$username"); } /***********END SPAM ALERT EMAIL*******************/ } else { //send off a different email } } Quote Link to comment Share on other sites More sharing options...
rmelino Posted March 4, 2011 Author Share Posted March 4, 2011 i got rid of the error but that isn't working either for the following reason: as i understand it the code is saying this on form submit: look at $full_string, if you find the first value in array $spam, then send the spam email notification, if not, send the all is ok email. Now, loop through again and look for the second word in array $spam. if the second word is in $full_string, send the spam alert email, otherwise, send an email that says all is ok, and so on and so on. The problem with this is, I want to check $full_string to see if any of the $spam array words exist. If they do, send a single spam alert email. If they don't, send a single 'all is ok' email. Does this make sense? Quote Link to comment Share on other sites More sharing options...
flolam Posted March 4, 2011 Share Posted March 4, 2011 $string = "testspamadoiviagraasde"; $spam = array("spam", "viagra"); $spam_found = false; foreach ($spam as $spamword) { if (strrpos($full_string, $spamword)) { $spam_found = true; break; } } if ($spam_found) { //send spam mail } else { //send okay mail } Quote Link to comment Share on other sites More sharing options...
rmelino Posted March 4, 2011 Author Share Posted March 4, 2011 perfect, that works! Thank you very much for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.