Question

I've been getting a lot of spam coming from my contact form recently, so I decided to add a little bit of validation to the "message" portion of the form. I've written a function that I thought would work to weed out the most commonly used words I see in my spammy emails from the form, but it's always returning false when I run it.

Here's the function:

function spamCheck($input) {
    $flags = array('cialis', 'viagra', 'erection', 'pharmac', 'porn', 'anal', 'bondage', 'insurance', 'ringtone', 'poker', 'casino', 'gambl', 'whore', 'nipple', 'shit', 'realt', 'shemale', 'valium');
    $input = explode(' ', $input);
    foreach($input as $word) {
        $word = trim($word, '",.!?\';:*');
        foreach($flags as $flag) {
            if(strpos(strtolower($word), $flag)) {
                return $word;
                exit;
            }
        }
    }
    return false;
}

You'll notice that some of the words in the blacklist aren't complete. Such as "gambl", which could be sent in an email as "gambling" or "gamble" or "gambler" or "gambles" or "gambled". This is why I was using strpos() from within the foreach loop, so that it would match partial strings, not just exact strings.

Any ideas what's causing this to fail?

Was it helpful?

Solution

The following should work:

function spamCheck($input){
    $flags = array('cialis', 'viagra', 'erection', 'pharmac', 'porn', 'anal', 'bondage', 'insurance', 'ringtone', 'poker', 'casino', 'gambl', 'whore', 'nipple', 'shit', 'realt', 'shemale', 'valium');
    $input = explode(' ', $input);
        foreach($input as $word){
        $word = trim($word, '",.!?\';:*');
            foreach($flags as $flag){
                if(strpos(strtolower($word), $flag) !== false){
                return $word;
                }
            }
        }
    return false;
}

$spam = spamCheck("Download free ringtones for your mobile phone.");
print($spam); // will print "ringtones"

Read more about the return values of strpos.

Update: The regex equivalent of your function:

function spamCheck($input){
    $flags = array('cialis', 'viagra', 'erection', 'pharmac', 'porn', 'anal', 'bondage', 'insurance', 'ringtone', 'poker', 'casino', 'gambl', 'whore', 'nipple', 'shit', 'realt', 'shemale', 'valium');
    $pattern = "/\\b[a-z]*(?:".implode("|", $flags).")[a-z]*\\b/i";
        if(preg_match($pattern, $input, $matches)){
        return $matches[0];
        }
    return false;
}

OTHER TIPS

You would be better off using regex here with preg_replace. Doing manual string searches will affect efficiency and leave a lot of edge cases unsolved.

Try this

function spamCheck($input) {
$flags = array('cialis', 'viagra', 'erection', 'pharmac', 'porn', 'anal', 'bondage', 'insurance', 'ringtone', 'poker', 'casino', 'gambl', 'whore', 'nipple', 'shit', 'realt', 'shemale', 'valium');
$input = explode(' ', $input);
foreach($input as $word) {
    $word = trim($word, '",.!?\';:*');
     foreach($flags as $fl){
      if(strpos($word,$fl) !== false ){ // edited as user requested
          return $word;
     }
}
}
return false;

}

A few modifications and it works:

    function spamCheck($input) {
    $flags = array('cialis', 'viagra', 'erection', 'pharmac', 'porn', 'anal', 'bondage', 'insurance', 'ringtone', 'poker', 'casino', 'gambl', 'whore', 'nipple', 'shit', 'realt', 'shemale', 'valium');

    $input = preg_replace('/[",.!?\';:*]/', '', $input);

    $input = explode(' ', $input);

    foreach($input as $word) {
      foreach($flags as $flag) {
        if(strpos(strtolower($word), $flag) !== false) {
          return $word;
        }
      }   
    }
    return false;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top