Question

I have some code to check is a form is being spammed and if so then stop the email.

It includes a section like this:

if(strpos($messagefield, " cialis") !== false){
$noemail = true;
}
if(strpos($messagefield, " viagra") !== false){
$noemail = true;
}

etc for as many words as we have in the bad word list

This works fine, but is clumsy and difficult to easily add new words to check. It would be easier if I could create an array and check any field against the array, but I am strugling to find an example to use (most examples still specify the text to search for which defeats the object in this case)

Can anyone help with code to check $messagefield against an array?

(I know there are better ways maybe but this works for us at the moment!)

Was it helpful?

Solution

$i = 0;
$wordlist = array(' cialis', ' viagra');

while ($i < count($wordlist) && $noemail == false) {
  if (strpos($messagefield, $wordlist[$i]) !== false) {
    $noemail = true;
  }
  $i++;
}

OTHER TIPS

  1. It is better to use stripos (case-insensitive version of strpos).
  2. Try following code:

    $a = array(' cialis', ' viagra'); for ($i = 0; $i < count($a); $i++) if(stripos($messagefield, $a[$i]) !== false){ $noemail = true; break; } }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top