How to pass a latin1 charset associative array from php to javascript?

为君一笑 提交于 2019-12-25 03:54:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!