问题
The problem is that I have latin1 charset in db. The default output is utf-8 in php. even when i am using php iconv, it returns null as the new string is supposedly already utf-8.
Converting DB would need downtime.
I cant use json_encode because the string is malformed utf-8 containing �.
I need an alternative to temporarily fix the issue without having to migrate the db.
I need to transfer the associative array back to JS. I am using Ajax for getting the data.
回答1:
You could convert the encoding, prior to passing the data on to json_encode (which really needs UTF8 encoded strings). Using the mb_convert_encoding function will do:
$utf8String = mb_convert_encoding(
$sourceString,
'UTF8',
'<src encoding>' //in your case: ISO-8859-1
);
Meanwhile, work out a strategy of how you are going to convert your DB to UTF-8, because it'll save you a lot of pain endlessly juggling different encodings throughout your project.
回答2:
Set your driver's encoding (client) explicitely to UTF-8.
Examples with:
- PDO/PostgreSQL
- PDO/MySQL
来源:https://stackoverflow.com/questions/25184917/how-to-pass-a-latin1-charset-associative-array-from-php-to-javascript