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!

 

Link to comment
Share on other sites

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
Share on other sites

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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.