I have an array that is json encoded and stored into a database. All my php files are utf8, and my mysql database is utf8_general_ci.
My array contains names which may contain accentuated character, which json_encode transform into \u characters. This array get stored into my database. When I use jQuery.parseJSON, the accentuated character show properly.
info = jQuery.parseJSON(data);
console.log(info);
//"Bob Épine" which is good
BUT when I want to use json_decode in PHP the \u characters stays.
print_r($this->opts['name']);
echo $this->opts['name'];
//Bob \u00c9pine
//Bob u00c9pine
I found out this function PHP: Replace \u characters in json string, is this the way to go?
It does work, but it seem to avoid the problem instead of fixing it.
Firstly, storing JSON in a DB table sounds like a flawed application design, but setting that aside for a second…
This sounds like Bobby Tables territory to me. By which I mean, it sounds like you are not escaping the
\when you insert the data into the database. This means it is getting stripped out of the string, so all you end up with isuxxxxinstead of\uxxxx, which means that they cannot be decoded.Now, ways around this are many and varied, but the first thing you need to do is to ensure that the data is inserted into the database correctly, and the encoded JSON string is identical when it comes out to the one you put it. If you do this and you find that you are still having a problem with this, I have a function I wrote a while ago which will convert unicode code points to their binary representations (which I am just finding for you)