Php json and array conversion support chinese_php tutorial-php Tutorial

Source: Internet
Author: User
Php json and arrays support Chinese conversion. Two functions are provided for me in php5. json_encode and json_decode can be used to convert json and arrays, but they cannot be supported in Chinese, the following describes two functions supported by processing a Chinese array. in php5, json_encode and json_decode can implement conversion between json and arrays. However, they do not support Chinese, the following describes a processing function that supports conversion between Chinese arrays and json.
The code is as follows:


/**
* Json generation and analysis support Chinese characters
*/
Class Json_Helper {
/**
* Generate json
*/
Public static function encode ($ str ){
$ Json = json_encode ($ str );
// Linux
Return preg_replace ("# \ u ([0-9a-f] {4}) # ie", "iconv ('ucs-2be ', 'utf-8', pack ('h4 ', '\ 1') ", $ json );
// Windows
// Return preg_replace ("# \ u ([0-9a-f] {4}) # ie", "iconv ('ucs-2le', 'utf-8 ', pack ('h4 ',' \ 1') ", $ json );
}

/**
* Json analysis
*/
Public static function decode ($ str ){
Return json_decode ($ str );
}
}
?>

...

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.