PHP/json:解码utf8?

发布于 2024-12-09 00:37:51 字数 440 浏览 1 评论 0原文

我在 mysql 数据库中存储了一个包含一些(中文?)字符的 json 字符串。 数据库中内容的示例:

normal.text.\u8bf1\u60d1.rest.of.text

在我的 PHP 页面上,我只是对从 mysql 收到的内容进行了 json_decode,但它显示不正确,它显示了诸如“½±è§�”之类的内容,

我尝试执行“ SET NAMES 'utf8'”查询位于我的文件开头,没有更改任何内容。 我的网页上已经有以下标头:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

当然,我所有的 php 文件都以 UTF-8 编码。

你知道如何很好地显示这些“\uXXXX”字符吗?

I store a json string that contains some (chinese ?) characters in a mysql database.
Example of what's in the database:

normal.text.\u8bf1\u60d1.rest.of.text

On my PHP page I just do a json_decode of what I receive from mysql, but it doesn't display right, it shows things like "½±è§�"

I've tried to execute the "SET NAMES 'utf8'" query at the beginning of my file, didn't change anything.
I already have the following header on my webpage:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

And of course all my php files are encoded in UTF-8.

Do you have any idea how to display these "\uXXXX" characters nicely?

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

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

发布评论

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

评论(3

超可爱的懒熊 2024-12-16 00:37:51

这似乎对我来说工作得很好,在 Ubuntu 11.04 上使用 PHP 5.3.5:

<?php
header('Content-Type: text/plain; charset="UTF-8"');
$json = '[ "normal.text.\u8bf1\u60d1.rest.of.text" ]';

$decoded = json_decode($json, true);

var_dump($decoded);

输出:

array(1) {
  [0]=>
  string(31) "normal.text.诱惑.rest.of.text"
}

This seems to work fine for me, with PHP 5.3.5 on Ubuntu 11.04:

<?php
header('Content-Type: text/plain; charset="UTF-8"');
$json = '[ "normal.text.\u8bf1\u60d1.rest.of.text" ]';

$decoded = json_decode($json, true);

var_dump($decoded);

Outputs this:

array(1) {
  [0]=>
  string(31) "normal.text.诱惑.rest.of.text"
}
黑凤梨 2024-12-16 00:37:51

Unicode 不是 UTF-8!

$ echo -en '\x8b\xf1\x60\xd1\x00\n' | iconv -f unicodebig -t utf-8
诱惑

这是一个奇怪的“编码”。我猜普通文本的每个字符都是“一个字节”长(US-ASCII)?然后,您必须提取 \u.... 序列,将序列转换为“两字节”字符,并使用 iconv("unicodebig", "utf-8", $character) 转换该字符 转换为 UTF-8 字符(请参阅 PHP 文档中的 iconv )。这对我来说很有效:

$in = "normal.text.\u8bf1\u60d1.rest.of.text";

function ewchar_to_utf8($matches) {
    $ewchar = $matches[1];
    $binwchar = hexdec($ewchar);
    $wchar = chr(($binwchar >> 8) & 0xFF) . chr(($binwchar) & 0xFF);
    return iconv("unicodebig", "utf-8", $wchar);
}

function special_unicode_to_utf8($str) {
    return preg_replace_callback("/\\\u([[:xdigit:]]{4})/i", "ewchar_to_utf8", $str);
}

echo special_unicode_to_utf8($in);

否则我们需要更多关于数据库中的字符串如何编码的信息。

Unicode is not UTF-8!

$ echo -en '\x8b\xf1\x60\xd1\x00\n' | iconv -f unicodebig -t utf-8
诱惑

This is a strange "encoding" you have. I guess each character of the normal text is "one byte" long (US-ASCII)? Then you have to extract the \u.... sequences, convert the sequence in a "two byte" character and convert that character with iconv("unicodebig", "utf-8", $character) to an UTF-8 character (see iconv in the PHP-documentation). This worked on my side:

$in = "normal.text.\u8bf1\u60d1.rest.of.text";

function ewchar_to_utf8($matches) {
    $ewchar = $matches[1];
    $binwchar = hexdec($ewchar);
    $wchar = chr(($binwchar >> 8) & 0xFF) . chr(($binwchar) & 0xFF);
    return iconv("unicodebig", "utf-8", $wchar);
}

function special_unicode_to_utf8($str) {
    return preg_replace_callback("/\\\u([[:xdigit:]]{4})/i", "ewchar_to_utf8", $str);
}

echo special_unicode_to_utf8($in);

Otherwise we need more Information on how your string in the database is encoded.

眉黛浅 2024-12-16 00:37:51

这是一个转移注意力的事情。如果您通过 http 提供页面,并且响应包含 Content-Type 标头,则元标记将被忽略。默认情况下,如果您没有明确设置,PHP 将设置这样的标头。默认设置为 iso-8859-1

尝试使用这一行:

<?php
header("Content-Type: text/html; charset=UTF-8");

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

That's a red herring. If you serve your page over http, and the response contains a Content-Type header, then the meta tag will be ignored. By default, PHP will set such a header, if you don't do it explicitly. And the default is set as iso-8859-1.

Try with this line:

<?php
header("Content-Type: text/html; charset=UTF-8");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文