数据表昵称字段设置属性为utf8mb4 utf8mb4_general_ci
存储数据的时候使用emoji_encode(nickname)
读取数据的时候使用emoji_decode(nickname)
直接清除表情的方法removeEmoji(nickname)
//对emoji表情转义
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;
}
}
// $strEncode = removeEmoji($strEncode);
return $strEncode;
}
//对emoji表情转反义
function emoji_decode($str)
{
$strDecode = preg_replace_callback('|\[\[EMOJI:(.*?)\]\]|', function ($matches) {
return rawurldecode($matches[1]);
}, $str);
return $strDecode;
}
//特殊字符二次排除
function removeEmoji($nickname) {
$clean_text = "";
// Match Emoticons
$regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
$clean_text = preg_replace($regexEmoticons, '', $nickname);
// Match Miscellaneous Symbols and Pictographs
$regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
$clean_text = preg_replace($regexSymbols, '', $nickname);
// Match Transport And Map Symbols
$regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
$clean_text = preg_replace($regexTransport, '', $nickname);
// Match Miscellaneous Symbols
$regexMisc = '/[\x{2600}-\x{26FF}]/u';
$clean_text = preg_replace($regexMisc, '', $nickname);
// Match Dingbats
$regexDingbats = '/[\x{2700}-\x{27BF}]/u';
$clean_text = preg_replace($regexDingbats, '', $nickname);
return $clean_text;
}