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

问题描述:

我从strace获得以下输出,我想使用PHP将其转换为UTF-8:

I have the following output from strace and i want to convert it to UTF-8 using PHP:

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

我认为上述字符串是UTF 16十六进制.

The above strings is UTF 16 HEX i think.

发现以下功能有效:

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/zh-CN的某些部分/function.urldecode.php