php – json_decode \u2019-ThrowExceptions

Exception or error:

I have a JSON file I am reading in through PHP and echoing out to an HTML file. Within the JSON file there is this string: Lisa\u2019s husband

That \u2019 is a “right single quotation mark” and should be output as such; instead, I get this: ’

I’ve tried specifying a UTF-8 encoding on my HTML page, but it doesn’t fix the problem–it just makes it different.

Without Specifying Encoding

’

Specifying a UTF-8 Encoding in the HTML File

�

Headers Sent

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

What do I need to do to get my apostrophe to look the right way? I do not want to replace these values with '

Edit

Specifying header('Content-Type: text/html; charset=utf-8'); changed my headers but did not fix the issue.

How to solve:

\u2019 is unicode or UTF-16. UTF-8 creates 8-bit character combinations like \x##\x##. You need to convert the string from unicode to utf-8 before parsing it with php json_decode. Finally after decoding it you have to convert the values to their htmlentities by directly calling the function (ie. if the field you want to decode is cars in races object.

<?php

// Convert from unicode to utf-8

// json_decode

echo htmlentities(races.cars[1])

?>

Leave a Reply

Your email address will not be published. Required fields are marked *