php简单过滤表情,有时候不想表情出现在名称或昵称中,对不是数据库不是utf8mb4的保存也不友好,那就过滤。emoji表情一般占4位字节。
<?php//过滤表情public function filterEmoji($str, $rep = '□'){$str = preg_replace_callback('/./u',function (array $match) use ($rep) {return strlen($match[0]) >= 4 ? $rep : $match[0];},$str);return $str;}
文章来源:https://www.weizhixi.com/user/index/article/id/87.html
附:php判断是否含有emoji表情
<?phpfunction have_special_char($str){$length = mb_strlen($str);$array = [];for ($i=0; $i<$length; $i++) {$array[] = mb_substr($str, $i, 1, 'utf-8');if( strlen($array[$i]) >= 4 ){return true;}}return false;}
