php - json_decode\u2019

标签 php html encoding json

我有一个 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/

相关文章:

php - 什么情况需要使用 eval() 因为别无选择?

php - 使用 mysqli 和 oops 创建数据库并在数据库中插入值

javascript - 当我将 SVG 内容绘制到 Canvas 时,SVG 被裁剪

javascript - 如何使用 JavaScript 查找 HTML 页面中的空格?

java - 使用 JAXB 解析器解析韩语字符串 작동불 时获取字符串 '???'

python - hashlib.md5() 类型错误 : Unicode-objects must be encoded before hashing

php - 如何在php中使用pdf库?

html - 在css中设置多个段落样式

python - Django:上传的文件编码问题

php - 我什么时候应该在没有 die() 的情况下调用 header ('Location' )?