Jump to content

look for certain words from array list within a string


rmelino

Recommended Posts

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!

 

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?

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?

 

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
}
}

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?

$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
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.