I implemented this "bad word" check function in php:
# bad word detector
function check_badwords($string) {
$badwords = array(a number of words some may find inappropriate for SE);
foreach($badwords as $item) {
if(stripos($string, $item) !== false) return true;
}
return false;
}
It works alright, except I'm having a little problem. If the $string is:
Who is the best guitarist ever?
...it returns true, because there is a match with Who ($string) and ho (in $badwords array). How could the function be modified so that it only checks for complete words, and not just part of words?
- check_badwords('She is a ho'); //should return true
- check_badwords('Who is she?'); //should return false
Thanks!
In order to check for complete words you should use regular expressions:
How the
regex
worksThe regular expression starts and ends with the special sequence
\b
that matches a word boundary (i.e. when a word character is followed by a non-word character or viceversa; the word characters are the letters, the digits and the underscore).Between the two word boundaries there is a subpattern that contains all the bad words separated by
|
. The subpattern matches any of the bad words.If you want to know what bad word was found you can change the function: