如何将unicode字符转换为后切值——以解决json_encode问题

时间:2021-07-14 06:27:37

I'm trying to pull sport players from my database that are already stored as unicode values. when calling json_encode it gives up when it hits unicode characters in the format i've got:

我试图从我的数据库中拉出一些已经存储为unicode值的运动播放器。当调用json_encode时,它遇到unicode字符时就放弃了我得到的格式:

$values = array('a'=>'BERDYCH, Tomáš','b'=>'FEDERER, Roger');
echo json_encode($values);

the result is

结果是

{"a":"BERDYCH, Tom","b":"FEDERER, Roger"}

{“a”:“伯蒂奇,汤姆”、“b”:“费德勒,罗杰”}

You can see 'Tom' was cut-off because it reached the unicode characters.

您可以看到“Tom”被切断,因为它到达unicode字符。

I understand json_encode only handles \uxxxx style characters but the problem is my database of thousands of sporting competitors already contains unicode stored values, so somehow I need to convert á type characters into \uxxxx without doing updates to my data source.

我知道json_encode只处理\uxxxx样式的字符,但问题是我的数据库中有数以千计的竞争对手已经包含unicode存储值,所以我需要在不更新数据源的情况下将一个类型字符转换为\uxxxx。

Any ideas?

什么好主意吗?

4 个解决方案

#1


1  

json_encode() does this when it gets characters that are not valid UTF-8 characters.

json_encode()在获取非有效的UTF-8字符时进行此操作。

If you are fetching data from the database, the most likely reason is that your connection is not UTF-8 encoded, and you are getting ISO-8859-1 data from your queries.

如果您正在从数据库中获取数据,最可能的原因是您的连接不是UTF-8编码的,并且您正在从查询中获得ISO-8859-1数据。

Show your database code for a suggestion how to change this.

显示您的数据库代码,以获得如何更改该命令的建议。

I understand json_encode only handles \uxxxx style characters

我知道json_encode只处理\uxxxx样式的字符

This is not true. json_encode() outputs Unicode characters encoded this way, but it doesn't expect them in the incoming data.

这不是真的。json_encode()输出编码为这种方式的Unicode字符,但它不会在传入的数据中期望它们。

#2


1  

Your source code and/or the data coming from the database is not encoded in UTF-8. I'd guess it's one of the specialized ISO-8859 encodings, but I'm not sure. When saving your source code, make sure it's saved in UTF-8. When getting data from the database, make sure you're setting the connection to utf8.

您的源代码和/或来自数据库的数据不是用UTF-8编码的。我想这是一个特殊的ISO-8859编码,但我不确定。保存源代码时,请确保它保存在UTF-8中。从数据库获取数据时,请确保将连接设置为utf8。

See What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and Handling Unicode Front To Back In A Web App.

要想在Web应用程序中使用文本和从头到尾地处理Unicode,一定要了解每个程序员绝对需要了解的编码和字符集。

#3


0  

To make sure they are UTF8, encode all values in your array

为了确保它们是UTF8,对数组中的所有值进行编码

$values = array_map('utf8_encode', $values);

If that doesn't help use mb_detect_encoding() and mb_convert_encoding() to change language specific encoding to UTF8.

如果这不能帮助使用mb_detect_encoding()和mb_convert_encoding()将特定语言的编码更改为UTF8。

#4


0  

It's a c# question, but take a look at Converting Unicode strings to escaped ascii string for an implementation that does this.

这是一个c#问题,但是看看如何将Unicode字符串转换为从ascii字符串转换为实现的方法。

#1


1  

json_encode() does this when it gets characters that are not valid UTF-8 characters.

json_encode()在获取非有效的UTF-8字符时进行此操作。

If you are fetching data from the database, the most likely reason is that your connection is not UTF-8 encoded, and you are getting ISO-8859-1 data from your queries.

如果您正在从数据库中获取数据,最可能的原因是您的连接不是UTF-8编码的,并且您正在从查询中获得ISO-8859-1数据。

Show your database code for a suggestion how to change this.

显示您的数据库代码,以获得如何更改该命令的建议。

I understand json_encode only handles \uxxxx style characters

我知道json_encode只处理\uxxxx样式的字符

This is not true. json_encode() outputs Unicode characters encoded this way, but it doesn't expect them in the incoming data.

这不是真的。json_encode()输出编码为这种方式的Unicode字符,但它不会在传入的数据中期望它们。

#2


1  

Your source code and/or the data coming from the database is not encoded in UTF-8. I'd guess it's one of the specialized ISO-8859 encodings, but I'm not sure. When saving your source code, make sure it's saved in UTF-8. When getting data from the database, make sure you're setting the connection to utf8.

您的源代码和/或来自数据库的数据不是用UTF-8编码的。我想这是一个特殊的ISO-8859编码,但我不确定。保存源代码时,请确保它保存在UTF-8中。从数据库获取数据时,请确保将连接设置为utf8。

See What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and Handling Unicode Front To Back In A Web App.

要想在Web应用程序中使用文本和从头到尾地处理Unicode,一定要了解每个程序员绝对需要了解的编码和字符集。

#3


0  

To make sure they are UTF8, encode all values in your array

为了确保它们是UTF8,对数组中的所有值进行编码

$values = array_map('utf8_encode', $values);

If that doesn't help use mb_detect_encoding() and mb_convert_encoding() to change language specific encoding to UTF8.

如果这不能帮助使用mb_detect_encoding()和mb_convert_encoding()将特定语言的编码更改为UTF8。

#4


0  

It's a c# question, but take a look at Converting Unicode strings to escaped ascii string for an implementation that does this.

这是一个c#问题,但是看看如何将Unicode字符串转换为从ascii字符串转换为实现的方法。