繁体   English   中英

如何在PHP中将UTF-16十六进制字符串转换为UTF-8?

[英]How to Convert UTF-16 hexadecimal string to UTF-8 in PHP?

我有来自strace的以下输出,我想使用PHP将其转换为UTF-8:

R\00f6dhakev\00e4gen 4
R\00e4ntm\00e4starv\00e4gen 24
K\00d8BENHAVN

我认为上面的字符串是UTF 16 HEX。

发现以下功能有效:

function utf8_urldecode($str) {

  $str = str_replace("\\00", "%u00", $str);

  $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));

  return html_entity_decode($str,null,'UTF-8');

}

一些部分来自http://us2.php.net/manual/en/function.urldecode.php

试试这个:

function masked_utf16_to_utf8($str) {
    $str = preg_replace_callback('/\\\\([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})/', create_function('$match', 'return mb_convert_encoding(chr(hexdec("$match[1]")).chr(hexdec("$match[2]")), "UTF-8", "UTF-16");');
    return $str;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM