문제

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!)

도움이 되었습니까?

해결책

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

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top