您可以使用数组并加入它
$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], '/'); // Automagically escape regex tokens (think about quantifiers +*, [], () delimiters etc...)
}
//print_r($arr); // Check the results after escaping
if(preg_match('/\b(?:' . join('|', $arr). ')\b/i', $string)) { // now we don't need to fear
echo 'banned words found';
} else {
echo $string;
}
它使用单词边界并连接数组.