《PHP應用:php實現utf-8轉unicode函數分享》要點:
本文介紹了PHP應用:php實現utf-8轉unicode函數分享,希望對您有用。如果有疑問,可以聯系我們。
代碼很簡單,功能卻很實用,保舉給大家.PHP應用
送上代碼先:PHP應用
代碼如下:
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);
}
《PHP應用:php實現utf-8轉unicode函數分享》是否對您有啟發,歡迎查看更多與《PHP應用:php實現utf-8轉unicode函數分享》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/12818.html