数据库编码格式使用utf8mb4
存入时先使用emoji_encode对内容进行编码
读取返回前端时使用emoji_decode解码即可
//对emoji表情转义
private function emoji_encode($str) {
$strEncode = '';
$length = mb_strlen($str, 'utf-8');
for ($i = 0; $i < $length; $i++) {
$_tmpStr = mb_substr($str, $i, 1, 'utf-8');
if (strlen($_tmpStr) >= 4) {
$strEncode .= '[[EMOJI:' . rawurlencode($_tmpStr) . ']]';
} else {
$strEncode .= $_tmpStr;
}
}
return $strEncode;
}
//对emoji表情转反义
private function emoji_decode($str) {
$strDecode = preg_replace_callback('|\[\[EMOJI:(.*?)\]\]|', function($matches) {
return rawurldecode($matches[1]);
}, $str);
return $strDecode;
}