views:

41

answers:

2

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

+1  A: 

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

Tseng
Ok, firts thanx, then I moved away the array_map but I got the same results: in my tests what I get is sometimes "??" and other times hex code, but never asiatic characater :(
Fabrizio
If you get something like \uxxxx (where xx are represented by hex numbers), then the encoding is correct. UTF and Unicode characters MUST BE encoded this way. If you convert this string into a JavaScript object (i.e. with eval(jsonString) and then print out the content of the fields it should print in asiatic characters
Tseng
If the encoding is correct I suppose that I need to decode client-side (on iPhone app), is that correct? Anyway big thanx for helping.
Fabrizio
Tseng FIXED. Thank you so much.
Fabrizio
Good to hear it worked out for you. Would be nice if you would accept the question as a small recognition. It increases the reputation ^^
Tseng
A: 

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.

Fabrizio