Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert php and json into Chinese

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to convert php and json into Chinese. It is very detailed and has a certain reference value. Interested friends must finish reading it!

Php json Chinese conversion methods: 1, the Chinese field for urlencode;2, using the json_encode function for coding; 3, with urldecode processing results.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer

How to convert a string in json format, two functions are provided in the php manual:

Json_encode: JSON encoding of variables

Json_decode: encodes strings in JSON format

Let's look at an example:

$arr = ['await,' baked,'c']; echo json_encode ($arr)

The output is:

["a", "b", "c"]

However, when we put Chinese in the array, there is a problem:

$arr = ['World', 'Hello',]; echo json_encode ($arr)

The result of the output is:

["\ u4e16\ u754c", "\ u4f60\ u597d"]

This result is obviously not what we want, but why is this happening? Because when our value contains Chinese, when php encodes him with json, the bottom layer encodes the Chinese with unicode, which makes the result unreadable.

What should we do about it?

Method one

You can use urlencode and urldecode methods to bypass the process of transcoding to unicode, first urlencode the Chinese fields, then json_encode, and finally use urldecode to process the results, and the Chinese language can be displayed normally. The specific code is as follows:

$arr = ['World', 'Hello',]; echo urldecode (json_encode ('urlencode', $arr)

The output is as follows:

["World", "Hello"]

Method two

Since the PHP5.4 version, an option has been officially added to Json: JSON_UNESCAPED_UNICODE. After adding this option, the Chinese character will not be encoded automatically. The specific code is as follows:

$arr = ['World', 'Hello',]; echo json_encode ($arr, JSON_UNESCAPED_UNICODE)

The output is:

["World", "Hello"]

See, this is what we want:)

Be careful

It is important to note that since json_encode and json_decode only support utf-8-encoded characters, gbk characters will have to be converted if they want to use the json function.

The above is all the contents of the article "how to convert php and json into Chinese". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report