中文关键字过滤时使用的关键字文件为utf-8编码 格式为key1|key2|key3|.... 解决中文和英文匹配的一些问题 /** * 被禁止的关键字检测 * * @param string $string 要检测的字符串 * @param string $fileName 屏蔽关键字文件 * @return bool */ function banwordCheck( $string, $fileName ) { if ( !($words = file_get_contents( $fileName )) ){ die('file read error!'); } $string = strtolower($string); $matched = preg_match('/'.$words.'/i', $string, $result); if ( $matched && isset($result[0]) && strlen($result[0]) > 0 ) { if ( strlen($result[0]) == 2 ){ $matched = preg_match('/'.$words.'/iu', $string, $result); } if ( $matched && isset($result[0]) && strlen($result[0]) > 0 ) { return true; }else{ return false; } }else{ return false; } } $content = '测试关键字'; if ( banwordCheck($content, './banwords.txt') ){ echo "matched! "; }else{ echo "no match! "; }