I have a script
below that detects for words in my word filter (an array), and determines whether a string
is clean or not.
What I have below works well when the words are used with spacing. But ifiwritesomething
without spaces, it doesn't detect.
How can I make it such that it searches the whole string instead of words? I tried removing the explode
function but I got some errors...
$string = 'goodmorningnoobs';
$array = array("idiot","noob");
if(0 == count(array_intersect(array_map('strtolower', explode(' ', $string)), $array))){
echo"clean";
} else {
echo "unclean";
}
Can anyone help?
$clean = true;
foreach ( $array as $word ) {
if ( stripos($string, $word) !== false ) {
$clean = false;
break;
}
}
echo $clean ? 'clean' : 'unclean';
How about?
$hasWords = preg_match('/'. implode('|', $words) .'/', $string);
echo $hasWords ? 'unclean' : 'clean';