How to read Asiatic characters (Japanese, Chinese) after json_encode in PHP
I've read every post about the topic but I don't think I've found a reply to my question, that's driving me crazy.
I got a couple of php files, one stores data into mySQL db, another one read those data: I get data from all over the world and it seems that I succeed to store asiatic character in 开发者_开发百科a right way, but when I try to read those data I can't get those characters back.
As many other users I got ?? instead of the correct chars.
Top of my PHP files I got:
header('Content-Type: application/json; charset=utf-8');
then
mysql_query("SET CHARACTER SET utf8", $link);
mysql_query("SET NAMES 'utf8'", $link);
then
$fab[] = array_map(utf8_encode,$array);
Here if I print_r ($fab)
I lost asiatic chars :-(
Then when I do:
$json_string = json_encode($fab); //originale
What I get is "??"
.
How is the correct way to get the right chars back? The json string is then passed to an iPhone client.
Any suggestion or help would be sooo appreciated.
Thank you anyway, Fabrizio
Seems like you're double encoding it? If you get the data from mysql which is already utf8 encoded, what's the point of $fab[] = array_map(utf8_encode,$array);
then?
Just had similar thing 2 days ago, when I was accepting utf8 data from an ExtJs form and it was messed up. It was cause I used utf8_encode on the data I received from the script (which was in utf8). So i broke it by double encoding. Maybe same in your case
The problem was what Tseng said: double encoding on the array: I thought I made the right test but simply I didn't.
So the only code I need is:
while($obj = mysql_fetch_object($rs)) { $arr[] = $obj; }
$json_string = json_encode($arr); echo ($json_string);
Again Tseng, thanx.
精彩评论