码迷,mamicode.com
首页 > 其他好文 > 详细

utf8 unicode 编码互转

时间:2017-08-27 12:47:17      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:ati   编码   param   nic   amp   function   utf8   log   cas   

static function utf8_to_unicode($c) {
        switch(strlen($c)) {
            case 1:
                return ord($c);
            case 2:
                $n = (ord($c[0]) & 0x3f) << 6;
                $n += ord($c[1]) & 0x3f;
                return $n;
            case 3:
                $n = (ord($c[0]) & 0x1f) << 12;
                $n += (ord($c[1]) & 0x3f) << 6;
                $n += ord($c[2]) & 0x3f;
                return $n;
            case 4:
                $n = (ord($c[0]) & 0x0f) << 18;
                $n += (ord($c[1]) & 0x3f) << 12;
                $n += (ord($c[2]) & 0x3f) << 6;
                $n += ord($c[3]) & 0x3f;
                return $n;
        }
    }

    /**
     * unicode转utf8
     * @param  $c
     */
    static function unicode_to_utf8($c) {
        $str = ‘‘;
        if($c < 0x80) {
            $str .= $c;
        } elseif($c < 0x800) {
            $str .= (0xC0 | $c >> 6);
            $str .= (0x80 | $c & 0x3F);
        } elseif($c < 0x10000) {
            $str .= (0xE0 | $c >> 12);
            $str .= (0x80 | $c >> 6 & 0x3F);
            $str .= (0x80 | $c & 0x3F);
        } elseif($c < 0x200000) {
            $str .= (0xF0 | $c >> 18);
            $str .= (0x80 | $c >> 12 & 0x3F);
            $str .= (0x80 | $c >> 6 & 0x3F);
            $str .= (0x80 | $c & 0x3F);
        }
        return $str;
    

 

utf8 unicode 编码互转

标签:ati   编码   param   nic   amp   function   utf8   log   cas   

原文地址:http://www.cnblogs.com/buxiangxin/p/7439932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!