Escaping JSON apostrophe before PHP's json_encode truncates?
Asked Answered
S

2

6

I have a field in a MySQL database (utf8_general_ci) that contains a curly (smart?) apostrophe: Owner’s...

This prints fine with no special handling if I access the PHP page that pulls it from the DB. However, I am trying to access it via a $.getJSON request on another page, so I used PHP's json_encode. It truncates the value so that it reads Owner, then successfully encodes the rest of the data. If I use PHP's utf8_encode on the field before I json_encode, it includes the full value with the encoded to \u0092 which then doesn't print anything on the page, giving me Owners. PHP's htmlentities and htmlspecialchars have no effect.

Looking at the request in Chrome's tools, Owner’s is shown as Owner�s on the $.getJSON page.

Can anyone help me out here? I have read other questions on SO and the web but I cannot find anything that helps and I haven't worked much with JSON.

Thanks for reading.

Seiden answered 26/2, 2013 at 19:23 Comment(4)
If I use PHP's utf8_encode on the field before I json_encode, it includes the full value with the ’ encoded to \u0092 that sounds like the way to go, that looks right. Can you elaborate on how that fails exactly? What does this scenario look like in Chrome's tools?Hispanicize
I think you need to set the encoding for the page you are outputting too. Also, you might need to decode the UTF-8.Jato
@Pekka: On the page, Owner\0092s is rendered as Owners, in Chrome's Tools, it looks like Owner\0092s. @crush, I am concerned this is not converting correctly because a search for \0092 indicates it is a control character (?) not an apostrophe? #11031351Seiden
Check out UTF-8 all the way through you may have a connection problem (ISO-8859-1 charset)Hispanicize
S
8

Using PHP's utf8_encode() before my json_encode() did indeed stop the data from cutting off after the but it also encoded it to \0092 which did not display (control character). When I used MySQL's SET NAMES utf8 before my query, I did not have to use utf8_encode() at all, and my json was encoded correctly with mapping to \u2019, which displays nicely.

Thanks for the link @Pekka, it helped me narrow down the possibilities.

Seiden answered 27/2, 2013 at 23:6 Comment(0)
A
11

For details: json_encode

Example:

echo json_encode($array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
Aymer answered 18/7, 2014 at 20:21 Comment(2)
@delive You may try with the necessary options JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR : php.net/manual/en/function.json-encode.phpAymer
Thanks. I didn't know about JSON_UNESCAPED_UNICODE.Araucanian
S
8

Using PHP's utf8_encode() before my json_encode() did indeed stop the data from cutting off after the but it also encoded it to \0092 which did not display (control character). When I used MySQL's SET NAMES utf8 before my query, I did not have to use utf8_encode() at all, and my json was encoded correctly with mapping to \u2019, which displays nicely.

Thanks for the link @Pekka, it helped me narrow down the possibilities.

Seiden answered 27/2, 2013 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.