Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this PHP call to json_encode silently failing - inability to handle single quotes?

Tags:

json

php

I have a stdClass object called $post that, when dumped via print_r(), returns the following:

stdClass Object (     [ID] => 12981     [post_title] => Alumnus' Dinner Coming Soon     [post_parent] => 0     [post_date] => 2012-01-31 12:00:51 ) 

Echoing the result from calling json_encode() on this object results in the following:

{     "ID": "12981",     "post_title": null,     "post_parent": "0",     "post_date": "2012-01-31 12:00:51" } 

I'm assuming that something with the single quote is causing json_encode to choke, but I don't know what format is needed to escape that. Any ideas?

EDIT: Fixed mismatch in code examples. I'm running PHP version 5.3.8

EDIT2: Directly after encoding the object, I did this:

echo json_last_error() == JSON_ERROR_UTF8; 

This printed 1, which means that the following error occurred: "Malformed UTF-8 characters, possibly incorrectly encoded". json_last_error()

EDIT3: Calling utf8_decode() on the post title resulted in the following: "Alumnus? Dinner Coming Soon". This data is being pulled from a MySQL database - the post title in particular is a text field, UTF-8 encoded. Maybe this single-quote is improperly encoded? The thing is, I have a SQL GUI app, and it appears correctly in that.

like image 398
rybosome Avatar asked Feb 01 '12 15:02

rybosome


People also ask

What does the PHP function json_encode () do?

PHP | json_encode() Function The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation. Parameters: $value: It is a mandatory parameter which defines the value to be encoded.

What is json_encode and Json_decode in PHP?

JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.

Is JSON decode slow?

Go JSON decoding is very slow.


1 Answers

You need to set the connection encoding before executing queries. How this is done depends on the API you are using to connect:

  • call mysql_set_charset("utf8") if you use the old, deprecated API.
  • call mysqli_set_charset("utf8") if you use mysqli
  • add the charset parameter to the connection string if you use PDO and PHP >= 5.3.6. In earlier versions you need to execute SET NAMES utf8.

When you obtain data from MySQL any text will be encoded in "client encoding", which is likely windows-1252 if you don't configure it otherwise. The character that is causing your problem is the "curly quote", seen as 92 in the hex dump, which confirms that the mysql client is encoding text in windows-1252.

Another thing you might consider is pass all text through utf8_encode, but in this case it wouldn't produce the correct result. PHP's utf8_encode converts iso-8859-1-encoded text. In this encoding \x92 is a non-printable control character, which would be converted into a non-printable control character in utf-8. You could use str_replace("\x92", "'", $input) to fix the problem for this particular character, but if there's any chance there will be any other non-ascii characters in the database you'll want to have the client use UTF-8.

like image 149
Joni Avatar answered Oct 04 '22 15:10

Joni