PHP 5.4 or earlier versions of json do not support solutions that do not escape content in Chinese. _ PHP Tutorial

Source: Internet
Author: User
Json versions earlier than php5.4 do not support solutions that do not escape Chinese content ,. PHP 5.4 or earlier versions of json do not support the solution of Chinese without escaping content. This article describes how to solve the problem that PHP 5.4 or earlier versions of json do not support Chinese without escaping content. The solution for json versions earlier than php5.4 does not support Chinese characters without escaping content,

This example describes how to solve the problem that json versions earlier than php5.4 do not support Chinese characters without escaping content. Share it with you for your reference. The specific analysis is as follows:

When writing the ERP interface, the JAVA side receives the content after json_encode.

The code is as follows:

{"OrderCode": "1401160935542399", "creator": "\ u751f \ u6d3b \ u7528 \ u54c1 \ u6d4b \ u8bd5 \ u5c0f \ u5c4b "}


"Creator": "\ u751f \ u6d3b \ u7528 \ u54c1 \ u6d4b \ u8bd5 \ u5c0f \ u5c4b" is in Chinese, and UTF8 is used currently. However, when the JAVA party receives such requests, it automatically converts the escaped Chinese characters back to the Chinese ones. the signature calculation method is based on this, and the signature is naturally not correct.

I checked the PHP Manual. The PHP version below 5.4 cannot escape Chinese characters, but the PHP version on our server is 5.3. so I used PHP to simulate a JSON method.

The code is as follows:

// Simulate joson without escaping
If (version_compare (PHP_VERSION, '5. 4.0 ')> = 0 ){
Function json_encode_ex ($ var ){
Return json_encode ($ var, JSON_UNESCAPED_UNICODE );
}
} Else {
Function json_encode_ex ($ var ){
If ($ var = null)
Return 'null ';

If ($ var = true)
Return 'true ';

If ($ var = false)
Return 'false ';

Static $ reps = array (
Array ("\", "/", "\ n", "\ t", "\ r", "\ B", "\ f ", '"',),
Array ('\', '\/', '\ n',' \ t', '\ r',' \ B ', '\ f ','\"',),
);

If (is_scalar ($ var ))
Return '"'. str_replace ($ reps [0], $ reps [1], (string) $ var ).'"';

If (! Is_array ($ var ))
Throw new Exception ('json encoder error! ');

$ IsMap = false;
$ I = 0;
Foreach (array_keys ($ var) as $ k ){
If (! Is_int ($ k) | $ I ++! = $ K ){
$ IsMap = true;
Break;
}
}

$ S = array ();

If ($ isMap ){
Foreach ($ var as $ k => $ v)
$ S [] = '"'. $ k. '":'. call_user_func (_ FUNCTION __, $ v );

Return '{'. implode (',', $ s ).'}';
} Else {
Foreach ($ var as $ v)
$ S [] = call_user_func (_ FUNCTION __, $ v );

Return '['. implode (',', $ s). ']';
}
}
}

You can use built-in functions directly. Json_encode_ex (array ('Diaoyu island '=> 'China'); multi-dimensional arrays are also supported.

I hope this article will help you with php programming.

Example: json of php5.4 or earlier does not support Chinese escape content. Share it with you...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.