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'));

  

posted @ 2016-09-19 11:23  Adtuu  阅读(415)  评论(0编辑  收藏  举报