我有一个 JSON 文件,我正在通过 PHP 读取该文件并将其输出到一个 HTML 文件。在 JSON 文件中有这个字符串:Lisa\u2019s husband
那个\u2019
是一个“右单引号”,应该这样输出;相反,我得到了这个:’
我试过了 specifying a UTF-8 encoding on my HTML page , 但它并没有解决问题——它只是让它变得不同。
不指定编码
'
在 HTML 文件中指定 UTF-8 编码
��
发送的 header
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:3918
Content-Type:text/html
Date:Mon, 19 Mar 2012 21:21:04 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=15, max=500
MS-Author-Via:DAV
Pragma:no-cache
Server:Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.7l PHP/5.2.15
X-Powered-By:PHP/5.2.15
我需要做什么才能使撇号看起来正确?我不想用 '
编辑
指定 header('Content-Type: text/html; charset=utf-8');
更改了我的 header ,但没有解决问题。
最佳答案
\u2019 是 unicode 或 UTF-16。 UTF-8 创建 8 位字符组合,如\x##\x##。在使用 php json_decode 解析之前,您需要将字符串从 unicode 转换为 utf-8。最后在解码之后,您必须通过直接调用函数将值转换为它们的 htmlentities(即,如果您要解码的字段是 races 对象中的汽车。
<?php
// Convert from unicode to utf-8
// json_decode
echo htmlentities(races.cars[1])
?>
关于php - json_decode\u2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9778290/