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

发布于 2024-07-30 10:11:03 字数 171 浏览 1 评论 0原文

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

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

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

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

The above strings is UTF 16 HEX i think.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凑诗 2024-08-06 10:11:04

尝试这个:

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;
}

Try this:

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;
}
九歌凝 2024-08-06 10:11:03

发现以下函数有效:

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');

}

来自 https://www.php 的某些部分。 net/manual/en/function.urldecode.php

Found that the following function works:

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');

}

Some parts from https://www.php.net/manual/en/function.urldecode.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文