没有设置数据库编码为utf8mb4时 emoji表情存储

//对emoji表情转义,存入时使用
    public 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表情转反义,取出数据时使用
    public function emoji_decode($str) {
       $strDecode = preg_replace_callback('|\[\[EMOJI:(.*?)\]\]|',               
  function($matches) {
           return rawurldecode($matches[1]);
       }, $str);
       return $strDecode;
    }
 
如果你设置了编码为utf8mb4还是存不了emoji表情你可以看看这篇文章http://www.lanxinbase.com/?p=1952

posted on 2021-01-08 09:33  沉淀物  阅读(101)  评论(0编辑  收藏  举报

导航