如何传递latin1字符集关联数组从PHP到JavaScript? [英] How to pass a latin1 charset associative array from php to javascript?

查看:174
本文介绍了如何传递latin1字符集关联数组从PHP到JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我在db中有latin1字符集。默认输出是utf-8在php。
即使我使用php iconv,它返回null,因为新的字符串据说已经是utf-8。

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.

转换数据库需要停机。

我不能使用json_encode,因为字符串格式错误utf-8包含 。

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.

关联数组返回JS。我使用Ajax获取数据。

I need to transfer the associative array back to JS. I am using Ajax for getting the data.

推荐答案

您可以在将数据传递到<$ c之前转换编码$ c> json_encode (这真的需要UTF8编码的字符串)。使用 mb_convert_encoding 函数将执行:

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
);

同时,制定一个如何将DB转换为UTF-8的策略,它会为你节省大量的痛苦,在你的整个项目中不断编译不同的编码。

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.

这篇关于如何传递latin1字符集关联数组从PHP到JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆