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

unicode_to_utf8 in PHP/**
unicode_to_utf8 in PHP    * 把Unicode的十六进制字符串转换成utf8的文本字符串
unicode_to_utf8 in PHP    * @param mixed $str 要转换的字符串,不能为null
unicode_to_utf8 in PHP    * @return utf8的文本字符串
unicode_to_utf8 in PHP
*/
unicode_to_utf8 in PHP
public static function unicode_hex_to_utf8($str)    {
unicode_to_utf8 in PHP    
//判断长度
unicode_to_utf8 in PHP
    if((strlen($str)%4!= 0 )
unicode_to_utf8 in PHP        
throw new exception('(strlen($str) % 4 != 0');
unicode_to_utf8 in PHP    
//计算byte[]的长度
unicode_to_utf8 in PHP
    $len = strlen($str)/4;
unicode_to_utf8 in PHP    
$str_result = '';
unicode_to_utf8 in PHP    
//循环复制
unicode_to_utf8 in PHP
    for($i=0;$i<$len;$i++){
unicode_to_utf8 in PHP        
$str_unicode_hex = substr($str, $i*4, 4);
unicode_to_utf8 in PHP        
$str_result .= self::unicode_to_utf8($str_unicode_hex);
unicode_to_utf8 in PHP    }
unicode_to_utf8 in PHP    
return $str_result;
unicode_to_utf8 in PHP}
unicode_to_utf8 in PHP
unicode_to_utf8 in PHP
private  static function unicode_to_utf8( $unicode_hex ) {
unicode_to_utf8 in PHP
unicode_to_utf8 in PHP    
$unicode = hexdec($unicode_hex);
unicode_to_utf8 in PHP
unicode_to_utf8 in PHP    
$utf8 = '';
unicode_to_utf8 in PHP
unicode_to_utf8 in PHP    
if ( $unicode < 128 ) {
unicode_to_utf8 in PHP        
$utf8 = chr$unicode );
unicode_to_utf8 in PHP
unicode_to_utf8 in PHP    } 
elseif ( $unicode < 2048 ) {
unicode_to_utf8 in PHP        
$utf8 .= chr192 + ( ( $unicode - ( $unicode % 64 ) ) / 64 ) );
unicode_to_utf8 in PHP        
$utf8 .= chr128 + ( $unicode % 64 ) );
unicode_to_utf8 in PHP
unicode_to_utf8 in PHP    } 
else {
unicode_to_utf8 in PHP
unicode_to_utf8 in PHP        
$utf8 .= chr224 + ( ( $unicode - ( $unicode % 4096 ) ) / 4096 ) );
unicode_to_utf8 in PHP        
$utf8 .= chr128 + ( ( ( $unicode % 4096 ) - ( $unicode % 64 ) ) /
unicode_to_utf8 in PHP        
64 ) );
unicode_to_utf8 in PHP        
$utf8 .= chr128 + ( $unicode % 64 ) );
unicode_to_utf8 in PHP
unicode_to_utf8 in PHP    } 
// if
unicode_to_utf8 in PHP
    return $utf8;
unicode_to_utf8 in PHP
// unicode_to_utf8

相关文章:

  • 2022-02-21
  • 2021-11-17
  • 2021-05-20
  • 2021-09-08
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2021-11-20
  • 2021-10-17
  • 2021-05-26
  • 2021-09-02
相关资源
相似解决方案