欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)utf-8轉(zhuǎn)unicode函數(shù)分享

 更新時(shí)間:2015年01月06日 14:51:37   投稿:hebedich  
這篇文章主要介紹了php實(shí)現(xiàn)utf-8轉(zhuǎn)unicode函數(shù)分享,需要的朋友可以參考下

代碼很簡(jiǎn)單,功能卻很實(shí)用,推薦給大家。

奉上代碼先:

復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評(píng)論