码迷,mamicode.com
首页 > Web开发 > 详细

php中utf-8转unicode

时间:2014-12-09 19:29:46      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

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);
}

参考文章:http://randomchaos.com/documents/?source=php_and_unicode

php中utf-8转unicode

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/lurenjiashuo/p/php-utf8-to-unicode.html

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