第一种解决办法直接过滤Emoji表情文字,方法如下
//格式化字符串
public function removeEmoji($text)
{
$text = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
'/./u',
function (array $match) {
return strlen($match[0]) >= 4 ? '' : $match[0];
},
$text);
$clean_text = "";
// Match Emoticons
$regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
$clean_text = preg_replace($regexEmoticons, '', $text);
// Match Miscellaneous Symbols and Pictographs
$regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
$clean_text = preg_replace($regexSymbols, '', $clean_text);
// Match Transport And Map Symbols
$regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
$clean_text = preg_replace($regexTransport, '', $clean_text);
// Match Miscellaneous Symbols
$regexMisc = '/[\x{2600}-\x{26FF}]/u';
$clean_text = preg_replace($regexMisc, '', $clean_text);
// Match Dingbats
$regexDingbats = '/[\x{2700}-\x{27BF}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
return $clean_text;
}
第二种,直接添加到数据库
因为mysql 的UTF-8最多3字节,而emoji表情是四字节,所以改成utf8mb4就能存储emoji
- windows下找到my.ini,添加如下
[mysqld]
character-set-server=utf8mb4
[mysql]
default-character-set=utf8mb4
- 如果是linux下,找到my.cnf,
linux查找文件的方式是 find / -name 'my.cnf'
添加如下
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
- 将已表也转换成utf8mb4
更改数据库编码:ALTER DATABASE 数据库名 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
更改数据表编码: alter table 表名 convert to character set utf8mb4 collate utf8mb4_bin;