You can use an array and join it
$arr = array('poker','casino','some','other', 'word', 'regex+++*much/escaping()');
$string = 'cool guy';
for($i = 0, $l = count($arr); $i < $l; $i++) {
$arr[$i] = preg_quote($arr[$i], '/');
}
if(preg_match('/\b(?:' . join('|', $arr). ')\b/i', $string)) {
echo 'banned words found';
} else {
echo $string;
}
It uses the word boundary and joins the array.
source
share