Search code examples
phparrayspreg-match

Need to match all word variations in this example


This will flag the words betting, gambling, xxx but it won't flag Betting, Gambling, XXX.

I'd like it to flag all variation of those words (i.e. Betting, BEttinG, BETTING, etc. etc.). Obviously, I can list them all separately, but I'd like to match them somehow so that all variations of those words would be flagged.

$original  = $_POST['message'];

$spamw = array('betting', 'gambling', 'xxx');

foreach ($spamw as $num) {
    if (( $pos = strpos( $original, $num )) !== false) { 
        $err = 'Something went wrong. Please, try again.';
    }
}

SOLOVED

$spamw = array('betting', 'gambling', 'xxx');
$original = $_POST['message'];

foreach ($spamw as $spamwords) {
  if (preg_match("/\b$spamwords\b/i", $original)) {
$err = 'Something went wrong. Please, try again.';
  }
}

Solution

  • One way to do this would be to store all of the words that are in the $spamw array as lowercase, like you're doing now, and then use the PHP strtolower() function to convert any user-input to lowercase characters. Your PHP script could then compare the user-input to the values stored in the $spamw array, checking to see if any of the words happen to match.