在项目开发中发现有个同事在作敏感词过滤的时候用循环在判断,实际上是不用这样作的,用php的数组函数和字符串函数便可实现php
function filterNGWords($string) { $badwords = array('fuck', 'cao', 'ri', 'ni ma'); $words = array_combine($badwords,array_fill(0,count($badwords),'*')); return strtr($string, $words); } var_dump(filterNGWords('wo ri ni ma'));