PHP使用unicode字符解码和编码json
我有一些 json 需要解码、更改然后编码而不弄乱任何字符。
如果我在 json 字符串中有 unicode 字符,它将不会解码。我不知道为什么,因为 json.org 说字符串可以包含:any-Unicode-character- except-"-or-\-or- control-character
。但它不适用于 我也
{"Tag":"Odómetro"}
可以使用 utf8_encode ,这将允许使用 json_decode 解码字符串,但是
[Tag] => Odómetro
当我再次对数组进行编码时, 该字符会被破坏成其他内容。字符转义为 ascii,根据 json 规范,这是正确的:
"Tag"=>"Od\u00f3metro"
有什么方法可以取消转义吗? json_encode 没有提供这样的选项,utf8_encode 似乎也不起作用
编辑 我明白了 。 json_encode 有一个 unescaped_unicode 选项,但是它没有按预期工作。哦,该死,它只在 php 5.4 上使用,因为我只需要使用一些正则表达式。有5.3。
$json = json_encode($array, JSON_UNESCAPED_UNICODE);
Warning: json_encode() expects parameter 2 to be long, string ...
I have some json I need to decode, alter and then encode without messing up any characters.
If I have a unicode character in a json string it will not decode. I'm not sure why since json.org says a string can contain: any-Unicode-character- except-"-or-\-or- control-character
. But it doesn't work in python either.
{"Tag":"Odómetro"}
I can use utf8_encode which will allow the string to be decoded with json_decode, however the character gets mangled into something else. This is the result from a print_r of the result array. Two characters.
[Tag] => Odómetro
When I encode the array again I the character escaped to ascii, which is correct according to the json spec:
"Tag"=>"Od\u00f3metro"
Is there some way I can un-escape this? json_encode gives no such option, utf8_encode does not seem to work either.
Edit I see there is an unescaped_unicode option for json_encode. However it's not working as expected. Oh damn, it's only on php 5.4. I will have to use some regex as I only have 5.3.
$json = json_encode($array, JSON_UNESCAPED_UNICODE);
Warning: json_encode() expects parameter 2 to be long, string ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我找到了以下方法来解决这个问题...我希望这可以帮助你。
I have found following way to fix this issue... I hope this can help you.
从您所说的一切来看,您正在处理的原始
Odómetro
字符串似乎是使用 ISO 8859-1 而不是 UTF-8 编码的。我这么认为的原因如下:
utf8_encode
运行输入字符串后,json_encode
生成可解析的输出,该字符串从 ISO 8859-1 转换为 UTF-8。utf8_encode
之后使用print_r
时,您得到了“损坏的”输出,但您得到的损坏的输出实际上正是尝试解析 UTF-8 时会发生的情况文本为 ISO 8859-1(ó 在 UTF-8 中为\x63\xb3
,但该序列在 ISO 8859-1 中为×
htmlentities
hackaround 解决方案有效。htmlentities
需要知道输入字符串的编码才能正常工作,如果您不指定,则假定为 ISO 8859-1。html_entity_decode
,令人困惑的是,默认为UTF-8,因此您的方法具有从ISO 8859-1转换为UTF-8的效果。)将使用
\uXXXX
转义,但正如您所指出的,这是有效的 JSON,因此,您似乎需要配置与 Postgres 的连接。它会给你 UTF-8 字符串。 PHP 手册表明你可以通过将
options='--client_encoding=UTF8'
附加到连接字符串来完成此操作。当前存储在数据库中的数据也可能采用错误的编码。 (您可以简单地使用utf8_encode
,但这仅支持属于 ISO 8859-1 一部分的字符)。最后,正如另一个答案所指出的,您确实需要确保使用 HTTP 标头或其他方式声明正确的字符集(当然,这个特定问题可能只是您执行
print_r
测试)。Judging from everything you've said, it seems like the original
Odómetro
string you're dealing with is encoded with ISO 8859-1, not UTF-8.Here's why I think so:
json_encode
produced parseable output after you ran the input string throughutf8_encode
, which converts from ISO 8859-1 to UTF-8.print_r
after doingutf8_encode
, but the mangled output you got is actually exactly what would happen by trying to parse UTF-8 text as ISO 8859-1 (ó is\x63\xb3
in UTF-8, but that sequence isó
in ISO 8859-1.htmlentities
hackaround solution worked.htmlentities
needs to know what the encoding of the input string to work correctly. If you don't specify one, it assumes ISO 8859-1. (html_entity_decode
, confusingly, defaults to UTF-8, so your method had the effect of converting from ISO 8859-1 to UTF-8.)PHP will use the
\uXXXX
escaping, but as you noted, this is valid JSON.So, it seems like you need to configure your connection to Postgres so that it will give you UTF-8 strings. The PHP manual indicates you'd do this by appending
options='--client_encoding=UTF8'
to the connection string. There's also the possibility that the data currently stored in the database is in the wrong encoding. (You could simply useutf8_encode
, but this will only support characters that are part of ISO 8859-1).Finally, as another answer noted, you do need to make sure that you're declaring the proper charset, with an HTTP header or otherwise (of course, this particular issue might have just been an artifact of the environment where you did your
print_r
testing).JSON_UNESCAPED_UNICODE
是在 PHP 5.4 中添加的,因此看起来您需要将 PHP 版本升级到利用它。 5.4 还没有发布! :(如果您想在开发机器上玩,QA 上有一个 5.4 alpha 候选版本。
JSON_UNESCAPED_UNICODE
was added in PHP 5.4 so it looks like you need upgrade your version of PHP to take advantage of it. 5.4 is not released yet though! :(There is a 5.4 alpha release candidate on QA though if you want to play on your development machine.
在 PHP 5.3 中执行 JSON_UNESCAPED_UNICODE 的一种黑客方法。对 PHP json 支持真的很失望。也许这会帮助别人。
A hacky way of doing JSON_UNESCAPED_UNICODE in PHP 5.3. Really disappointed by PHP json support. Maybe this will help someone else.
你很接近,只需使用 utf8_decode 即可。
You were close, just use utf8_decode.
尝试在页面中设置
utf-8
编码:这对我有用:
try setting the
utf-8
encoding in your page:this works for me:
尝试使用:
Try Using:
将包含特殊字符的数组从 ISO 8859-1 编码为 UTF8。 (如果 utf8_encode 和 utf8_decode 不适合您,这可能是一个选项)
ISO-8859-1 中的所有内容都应转换为 UTF8:
在此之后编码应该起作用:
将 UTF-8 转换为 &来自 ISO 8859-1
To encode an array that contains special characters, ISO 8859-1 to UTF8. (If utf8_encode & utf8_decode is not what is working for you, this might be an option)
Everything that is in ISO-8859-1 should be converted to UTF8:
Encode should work after this:
Convert UTF-8 to & from ISO 8859-1