Home Backend Development PHP Tutorial Solution to unicode uxxxx appearing in json_encode array_PHP tutorial

Solution to unicode uxxxx appearing in json_encode array_PHP tutorial

Jul 21, 2016 pm 02:57 PM
encode json unicode Do Appear and application Weibo array of solution

During the Dragon Boat Festival and the Weibo application gift giving done last weekend, I designed the data format of ajax to return json. I did not completely use PHP’s default json_encode to encode, because the coded code is unicode. That is the encoding of u. Although unicode encoding can be encoded in different pages, there will be no garbled problem. However, if a Chinese character is encoded into Unicode, it will become u+4 characters, which is longer than the Chinese character.

Because I use UTF-8 in my php files and html statements, there will be no encoding garbled problems, so I gave up the method of using json_encode directly, and instead urlencode the Chinese characters first and then use json_encode , use urldecode again after json_encode to decode, so that the Chinese characters in the encoded json array will not be unicode encoded~

The code is as follows

//The default is: {"test":"u6211u662fu6d4bu8bd5"}
$array = array(
'test'=>urlencode("I am a test")
);
$array = json_encode($array);
echo urldecode($array);
//{"test":"I am a test"}

This is mainly to save the number of transmitted characters, because my gift-giving will introduce hundreds of friend information by default, and the data traffic is still relatively large~ Therefore, using Chinese character transmission will save bandwidth than unicode character encoding transmission~ Moreover, the page encoding problem has been solved, and there will be no garbled characters.

The problem of null in json_encode
If the document encoding or string encoding (for example, UTF-8 captures a GBK page) is non-UTF-8, the problem of json_encode encoding failure will occur, and the output Chinese characters will be null.

The solution is to use the iconv function to convert Chinese characters to UTF-8 before json_encode.

Encoding of compressed files of TBCompressor
TBCompressor is YUICompressor modified by Taobao UED team. It can support converting Chinese characters in js and css into unicode encoding. If there are too many Chinese characters in the js file, after compression On the contrary, there will be a problem of increasing the size. For example, a js is an array of place names of provinces, cities and counties across the country, which needs to be solved by modifying the TBCompressor configuration~
We can solve it by modifying the cmd file of TBCompressor without using native2ascii. In this way, you can also use TBCompressor on a computer that does not have JDK installed but only JRE. Of course, if the js and css files you want to compress are UTF-8 encoded, you need to modify the charset in TBCompressor to UTF-8. .

I’m going too far, let’s call it a day~

Original text: http://www.js8.in/697.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363961.htmlTechArticleDragon Boat Festival and the Weibo application I made last weekend to give gifts were designed into the data format of ajax returning json. I It is not fully encoded using PHP's default json_encode, because the encoding is...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Jun 03, 2024 pm 01:25 PM

Common challenges faced by machine learning algorithms in C++ include memory management, multi-threading, performance optimization, and maintainability. Solutions include using smart pointers, modern threading libraries, SIMD instructions and third-party libraries, as well as following coding style guidelines and using automation tools. Practical cases show how to use the Eigen library to implement linear regression algorithms, effectively manage memory and use high-performance matrix operations.

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

Java framework security vulnerability analysis and solutions Java framework security vulnerability analysis and solutions Jun 04, 2024 pm 06:34 PM

Analysis of Java framework security vulnerabilities shows that XSS, SQL injection and SSRF are common vulnerabilities. Solutions include: using security framework versions, input validation, output encoding, preventing SQL injection, using CSRF protection, disabling unnecessary features, setting security headers. In actual cases, the ApacheStruts2OGNL injection vulnerability can be solved by updating the framework version and using the OGNL expression checking tool.

How to use PHP functions to process JSON data? How to use PHP functions to process JSON data? May 04, 2024 pm 03:21 PM

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values ​​of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

See all articles