php 微信用户昵称对emoji表情转义

    
//对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;
            }
        }

        return $strEncode;
    }
    //对emoji表情转反义
    function emoji_decode($str){
        $strDecode = preg_replace_callback('|\[\[EMOJI:(.*?)\]\]|', function($matches){
            return rawurldecode($matches[1]);
        }, $str);
        return $strDecode;
    }

使用上面的方法就可以对微信用户昵称进行处理保存到数据库中了,再也不用担心有的用户昵称保存不了的情况了


猜你喜欢

转载自blog.csdn.net/qq_35349114/article/details/80376616