• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

utf8_to_unicode in PHP

php 搞代码 3年前 (2022-01-23) 15次浏览 已收录 0个评论

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

/* *
* 从UTF8转换成unicode beta1.0
* @param mixed $string要转换的字符串,
* @return unicode的十六进制编码
*/
function utf8_to_unicode_hex( $string ){
$length = strlen ( $string );
$outstring = “” ;
for ( $i = 0 ; $i < $length ; $i ++ ) {
$asc_value = ord ( $string [ $i ]);
if ( $asc_value > 127 ) {
if ( $asc_value >= 192 && $asc_value <= 223 ){
$str_dec = ( ord ( $string [ $i ]) & 0x3f ) << 6 ;
$i ++ ;
$str_dec += ord ( $string [ $i ]) & 0x3f ;
$str_hex = dechex ( $str_dec )%本文来源gaodai#ma#com搞*代#码9网#搞gaodaima代码;
$outstring .= str_pad ( $str_hex , 4 , ” 0 ” , STR_PAD_LEFT);
} elseif ( $asc_value >= 224 && $asc_value <= 239 ){
$str_dec = ( ord ( $string [ $i ]) & 0x1f ) << 12 ;
$i ++ ;
$str_dec += ( ord ( $string [ $i ]) & 0x3f ) << 6 ;
$i ++ ;
$str_dec += ord ( $string [ $i ]) & 0x3f ;
$outstring .= dechex ( $str_dec );
} elseif ( $asc_value >= 240 && $asc_value <= 247 ){
$str_dec = ( ord ( $string [ $i ]) & 0x0f ) << 18 ;
$i ++ ;
$str_dec += ( ord ( $string [ $i ]) & 0x3f ) << 12 ;
$i ++ ;
$str_dec += ( ord ( $string [ $i ]) & 0x3f ) << 6 ;
$i ++ ;
$str_dec += ord ( $string [ $i ]) & 0x3f ;
$outstring .= dechex ( $str_dec );
} else {
$str_hex = dechex ( ord ( $string [ $i ]));
$outstring .= str_pad ( $str_hex , 4 , ” 0 ” , STR_PAD_LEFT);
}
} else {
$str_hex = dechex ( ord ( $string [ $i ]));
$outstring .= str_pad ( $str_hex , 4 , ” 0 ” , STR_PAD_LEFT);
}
}
return $outstring ;
}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:utf8_to_unicode in PHP
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址