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! Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/ 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"; } } Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1182979 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 Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1182990 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; Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1182991 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? Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1182994 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. Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1182998 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. Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1182999 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 protected]", $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? Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1183000 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 protected]", $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 } } Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1183004 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? Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1183012 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 } Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1183018 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 Link to comment https://forums.phpfreaks.com/topic/229612-look-for-certain-words-from-array-list-within-a-string/#findComment-1183026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.