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['value']);
//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_encodestates 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-8and see if it makes any difference.Assuming the connection character set is indeed UTF-8,
json_encodeshould work just fine. Then, you still have the final issue of converting the encoded data to ISO-8859-1. For example: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_contentsto 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.