PHP json_encode () Chinese encoding problem

Source: Internet
Author: User

This article lists 3 methods for implementing Json_encode () string to display the Chinese problem.

It's not necessary to do the interface, but it helps to save the log.

The official param and return must be affixed before the code is posted, link: http://php.net/manual/zh/function.json-encode.php

Parameters
value

To encode value , in addition to the resource type, can be any data type

This function can only accept UTF-8 encoded data.

options

A binary mask consisting of the following constants: JSON_HEX_QUOT , JSON_HEX_TAG JSON_HEX_AMP JSON_HEX_APOS JSON_NUMERIC_CHECK JSON_PRETTY_PRINT JSON_UNESCAPED_SLASHES JSON_FORCE_OBJECT JSON_UNESCAPED_UNICODE ,,,,,,,.

return value

Successful encoding returns a string that is represented as JSON or returned FALSE on failure.

<?php//Json_encode () Keep the Chinese method detailed $arr[' City ' = ' Beijing '; $arr [' name '] = ' weilong ';//Direct output//Res: {"City": "\u5317\u4eac", " Name ":" Weilong "}echo Json_encode ($arr)," \ n ";//1.  Add parameter, PHP version >5.4.0//Res: {"City": "Beijing", "name": "Weilong"}echo json_encode ($arr, Json_unescaped_unicode), "\ n"; php > 5.4.0//2. Regular replace, after Json_encode, the code is replaced with a Chinese//Res: {"City": "Beijing", "name": "Weilong"}echo preg_replace ("#\\\u ([0-9a-f]{4}) #ie", " Iconv (' ucs-2be ', ' UTF-8 ', pack (' H4 ', ' \\1 ') ", Json_encode ($arr))," \ n ";//3. UrlDecode (), UrlEncode () function, not recommended//Res1:null, Res2: {"City": "Beijing", "name": "Weilong"}echo UrlDecode (Json_encode (  UrlEncode ($arr))), "\ n"; $arr [' city '] = UrlEncode ($arr [' City ']); The UrlEncode () parameter must be Stringecho UrlDecode (Json_encode ($arr)), "\ n", or//note the difference between the Json_decode () parameter. $arr [' city '] = ' Beijing '; $arr [' name '] = ' weilong '; $str = Json_encode ($arr); $str 2 = Json_decode ($STR); $str 3 = Json_decode ($str , true);p Rint_r ($str 2); object/* Res:stdclass Object ([City] = Beijing [name] = Weilong) */print_r ($str 3); array/* Res:array ([city] = Beijing [name] = weilong) */ 

  

PHP json_encode () Chinese encoding problem

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.