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.'; } } Code (markup):
Just a quick note: The way you search for banned words will produce false alarms in some scenarios. For example, let's say you add word sex to banned words list. Now Try to submit text "I like loud sexto music" and see what happens - it will be flagged as spam. It's just a basic example, but I hope you got the idea. Once you start adding more words to banned words list, the number of false alarms will increase.
You can use preg_match to find blacklisted words. Using this function, you can match whole words, so if some long words contains a part of blacklisted word, no false alarm will be triggered.
P.S. Using preg_match, be sure to use /i flag and remove strtolower from your $original = strtolower($_POST['message']); code because this way you overwrite whole message just to scan for blacklisted words. In other words, if user submits a message "Hello, my name is John, I live in New York", you will see it as "hello, my name is john, i live in new york" if you continue using strtolower. However, /i flag in preg_match will allow you to match both UPPERCASE/lowercase words, but will NOT overwrite original message.
With preg_match: $spamw = array('betting', 'gambling', 'xxx'); $original = $_POST['message']; foreach ($spamw as $num) { if (preg_match("/\b$num\b/i", $original)) { $err = 'Something went wrong. Please, try again.'; } } Code (markup):