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.
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.
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.
Go JSON decoding is very slow.
You need to set the connection encoding before executing queries. How this is done depends on the API you are using to connect:
mysql_set_charset("utf8")
if you use the old, deprecated API.mysqli_set_charset("utf8")
if you use mysqli 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With