Heim > Backend-Entwicklung > PHP-Tutorial > unicode_to_utf8 in PHP

unicode_to_utf8 in PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Freigeben: 2016-06-23 14:36:31
Original
1367 Leute haben es durchsucht

当短信格式为8:UCS2编码的时候,需要把Unicode的十六进制编码转换成UTF8,但是PHP没有内置Unicode的支持,怎么办呢?

/* *
    * 把Unicode的十六进制字符串转换成utf8的文本字符串
    * @param mixed $str 要转换的字符串,不能为null
    * @return utf8的文本字符串
*/
public   static   function  unicode_hex_to_utf8( $str )    {
     // 判断长度
     if (( strlen ( $str ) % 4 )  !=   0  )
         throw   new   exception ( ' (strlen($str) % 4 != 0 ' );
     // 计算byte[]的长度
     $len   =   strlen ( $str ) / 4 ;
     $str_result   =   '' ;
     // 循环复制
     for ( $i = 0 ; $i          $str_unicode_hex   =   substr ( $str ,   $i * 4 ,   4 );
         $str_result   .=  self :: unicode_to_utf8( $str_unicode_hex );
    }
     return   $str_result ;
}

private    static   function  unicode_to_utf8(  $unicode_hex  ) {

     $unicode   =   hexdec ( $unicode_hex );

     $utf8   =   '' ;

     if  (  $unicode            $utf8   =   chr (  $unicode  );

    }  elseif  (  $unicode            $utf8   .=   chr (  192   +  ( (  $unicode   -  (  $unicode   %   64  ) )  /   64  ) );
         $utf8   .=   chr (  128   +  (  $unicode   %   64  ) );

    }  else  {

         $utf8   .=   chr (  224   +  ( (  $unicode   -  (  $unicode   %   4096  ) )  /   4096  ) );
         $utf8   .=   chr (  128   +  ( ( (  $unicode   %   4096  )  -  (  $unicode   %   64  ) )  /
         64  ) );
         $utf8   .=   chr (  128   +  (  $unicode   %   64  ) );

    }  //  if
     return   $utf8 ;
}  //  unicode_to_utf8

Verwandte Etiketten:
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
CAD-Datei in PHP öffnen?
Aus 1970-01-01 08:00:00
0
0
0
Warum ist 0==null in PHP wahr?
Aus 1970-01-01 08:00:00
0
0
0
Wie installiere ich PHP in Yum Nginx?
Aus 1970-01-01 08:00:00
0
0
0
Installieren Sie PHP in einer Windows-Umgebung
Aus 1970-01-01 08:00:00
0
0
0
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage