1、修改数据库的字符支持,utf8==>utf8mb4
2、匹配出emoji表情,替换与还原
/**
* 表情转换 更新到ios9.2涵盖编码范围
* @param $str
* @return mixed
*/
public static function emoji_to_html($str) {
$regex = ‘/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}][\x{FE00}-\x{FEFF}]?|[\x{1F900}-\x{1F9FF}][\x{FE00}-\x{FEFF}]?/u‘;
$str = preg_replace_callback($regex,function($matches){
$str = json_encode($matches[0]);
$str = ‘<em data-emoji=‘ . str_replace(‘\u‘, ‘em:‘, $str) . ‘></em>‘;
return $str;
},$str);
return $str;
}
从数据库取出后再转回
$string = preg_replace_callback(‘/<em data-emoji=\"(.*?)\"><\/em>/is‘, "self::preg_emoji", $string);
/**
* 输出emoji表情
* @param $matches
* @return mixed
*/
public static function preg_emoji($matches)
{
$str = $matches[0];
$str = str_replace(‘em:‘, ‘\u‘, $str);
return $str;
}
3、直接过滤emoji表情
/**
* 过滤表情
* @param $str
* @return mixed
*/
public static function filter_emoji($str) {
$regex = ‘/(\\\u[ed][0-9a-f]{3})/i‘;
$str = json_encode($str);
$str = preg_replace($regex, ‘‘, $str);
return json_decode($str);
}