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

utf8_to_unicode in PHP/**
utf8_to_unicode in PHP * 从UTF8转换成unicode beta1.0
utf8_to_unicode in PHP * @param mixed $string要转换的字符串,
utf8_to_unicode in PHP * @return unicode的十六进制编码
utf8_to_unicode in PHP
*/
utf8_to_unicode in PHP
function utf8_to_unicode_hex($string){
utf8_to_unicode in PHP    
$length = strlen($string);
utf8_to_unicode in PHP    
$outstring = "";
utf8_to_unicode in PHP    
for ( $i = 0$i < $length$i++ )  {
utf8_to_unicode in PHP        
$asc_value = ord($string[$i]);
utf8_to_unicode in PHP        
if($asc_value > 127) {
utf8_to_unicode in PHP            
if($asc_value >= 192 && $asc_value <= 223){
utf8_to_unicode in PHP                
$str_dec = (ord($string[$i]) & 0x3f<< 6;
utf8_to_unicode in PHP                
$i++;
utf8_to_unicode in PHP                
$str_dec += ord($string[$i]) & 0x3f;
utf8_to_unicode in PHP                
$str_hex = dechex($str_dec);
utf8_to_unicode in PHP                
$outstring .= str_pad($str_hex,4,"0",STR_PAD_LEFT);
utf8_to_unicode in PHP            }
elseif($asc_value >= 224 && $asc_value <= 239){
utf8_to_unicode in PHP                
$str_dec = (ord($string[$i]) & 0x1f<< 12;
utf8_to_unicode in PHP                
$i++;
utf8_to_unicode in PHP                
$str_dec += (ord($string[$i]) & 0x3f<< 6;
utf8_to_unicode in PHP                
$i++;
utf8_to_unicode in PHP                
$str_dec += ord($string[$i]) & 0x3f;
utf8_to_unicode in PHP                
$outstring .=dechex($str_dec);
utf8_to_unicode in PHP            }
elseif($asc_value >= 240 && $asc_value <= 247){
utf8_to_unicode in PHP                
$str_dec = (ord($string[$i]) & 0x0f<< 18;
utf8_to_unicode in PHP                
$i++;
utf8_to_unicode in PHP                
$str_dec += (ord($string[$i]) & 0x3f<< 12;
utf8_to_unicode in PHP                
$i++;
utf8_to_unicode in PHP                
$str_dec += (ord($string[$i]) & 0x3f<< 6;
utf8_to_unicode in PHP                
$i++;
utf8_to_unicode in PHP                
$str_dec += ord($string[$i]) & 0x3f;
utf8_to_unicode in PHP                
$outstring .= dechex($str_dec);
utf8_to_unicode in PHP            }
else{
utf8_to_unicode in PHP                
$str_hex = dechex(ord($string[$i]));
utf8_to_unicode in PHP                
$outstring .= str_pad($str_hex,4,"0",STR_PAD_LEFT);
utf8_to_unicode in PHP            }
utf8_to_unicode in PHP        }
else{
utf8_to_unicode in PHP            
$str_hex = dechex(ord($string[$i]));
utf8_to_unicode in PHP            
$outstring .= str_pad($str_hex,4,"0",STR_PAD_LEFT);
utf8_to_unicode in PHP        }
utf8_to_unicode in PHP    }
utf8_to_unicode in PHP    
return $outstring;
utf8_to_unicode in PHP}

相关文章:

  • 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
相关资源
相似解决方案