开发者

Another charset problem with php and MySQL

I'm having a problem with some characters like 'í' or 'ñ' working in a web project with PHP and MySQL.

The database table is in UTF-8 charset and the web page is ISO-8859-1 (latin-1). at first look everything is handled ok, but a problem is coming when I use the JSON_ENCODE function of PHP.

When I get a query result, let's say, this row:

 | ID | VALUE |
 --------------------
 | 1 | Línea |

I got the following (correct) array in PHP:

 Array("ID"=>"1","VALUE"=>"Línea");

So far, so good. But, when i apply the JSON_ENCODE

 $result = json_encode($result);
 //$result is {"id":"1","value":"L"}

Then i tried some coding/decoding but i couldn't get the right result.

First I tried to decode the UTF-8 chars like follow:

 $result['value'] = utf8_decode($result['v开发者_如何学编程alue']);
 //and I get $result['value'] is "L?a"

Then I tried with mb functions:

 $result['value'] = mb_convert_encoding($result['value'],"ISO-8859-1","UTF-8");
 //and I get that $result['value'] is "Lnea"

I don't really know why is the Json_encode breaking my string and i can't figure out what else to try. I will appreciate any help :)

Thanks!


The documentation for json_encode states that the function will only work on UTF-8 data. If it's not working for you, it means that your data is not UTF-8.

To understand what's going wrong, you need to know what your connection character set is. Is it UTF-8? Something else? Use SET NAMES utf-8 and see if it makes any difference.

Assuming the connection character set is indeed UTF-8, json_encode should work just fine. Then, you still have the final issue of converting the encoded data to ISO-8859-1. For example:

// assume any strings in $result are UTF-8 encoded
$json = json_encode($result);

$output = mb_convert_encoding($json, 'ISO-8859-1', 'UTF-8');

echo $output;

If it still doesn't work, it means that your UTF-8 strings contain characters not available in the ISO-8859-1 character set. There's nothing you can do about that.

Update:

When debugging complex character set conversions like this, you can use file_put_contents to write intermediate results to a file which you can inspect with a hex editor. This will help confirm that the output of a particular step of the process is correct or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜