当前位置: 移动技术网 > IT编程>开发语言>PHP > php实现utf-8转unicode函数分享

php实现utf-8转unicode函数分享

2018年07月07日  | 移动技术网IT编程  | 我要评论

黄梅人,九游战歌网,cscdkey序列号

代码很简单,功能却很实用,推荐给大家。

奉上代码先:

复制代码 代码如下:

public function utf8_unicode($str) {
    $unicode = array();
    $values = array();
    $lookingfor = 1;
    for ($i = 0; $i < strlen( $str ); $i++ ) {
        $thisvalue = ord( $str[ $i ] );
        if ( $thisvalue < ord('a') ) {
            // exclude 0-9
            if ($thisvalue >= ord('0') && $thisvalue <= ord('9')) {
                 // number
                 $unicode[] = chr($thisvalue);
            }
            else {
                 $unicode[] = '%'.dechex($thisvalue);
            }
        } else {
            if ( $thisvalue < 128) {
                $unicode[] = $str[ $i ];
            } else {
                if ( count( $values ) == 0 ) {
                    $lookingfor = ( $thisvalue < 224 ) ? 2 : 3;
                }
                $values[] = $thisvalue;
                if ( count( $values ) == $lookingfor ) {
                    $number = ( $lookingfor == 3 ) ?
                        ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
                        ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
                    $number = dechex($number);
                    $unicode[] = (strlen($number)==3)?"\u0".$number:"\u".$number;
                    $values = array();
                    $lookingfor = 1;
                } // if
            } // if
        }
    } // for
    return implode("",$unicode);
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网