Solve the Problem of gbk Chinese garbled characters passed to JavaScript and json_encode through PHP Arrays

Source: Internet
Author: User

This article introduces how to solve the gbk garbled characters passed by PHP arrays to JavaScript and json_encode. The following describes how to create a JSON function.

The Code is as follows: Copy code

/*************************************** ***********************
*
* Use a specific function to process all elements in the array
* @ Param string & $ array the string to be processed
* @ Param string $ function the function to be executed
* @ Return boolean $ apply_to_keys_also whether it is also applied to the key
* @ Access public
*
**************************************** *********************/
Function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false)
{
Foreach ($ array as $ key => $ value ){
If (is_array ($ value )){
ArrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also );
} Else {
$ Array [$ key] = $ function ($ value );
}

If ($ apply_to_keys_also & is_string ($ key )){
$ New_key = $ function ($ key );
If ($ new_key! = $ Key ){
$ Array [$ new_key] = $ array [$ key];
Unset ($ array [$ key]);
}
}
}
}

/*************************************** ***********************
*
* Convert an array to a JSON string (compatible with Chinese characters)
* @ Param array $ array the array to be converted
* @ Return string the converted json string
* @ Access public
*
**************************************** *********************/
Function JSON ($ array ){
ArrayRecursive ($ array, 'urlencode', true );
$ Json = json_encode ($ array );
Return urldecode ($ json );
}

Database Connection value to array $ array1

The Code is as follows: Copy code

$ Dbcnx = @ mysql_connect ("localhost", "root", "1234 ");
If (! $ Dbcnx ){
Echo ("Unable to connect to the". "database server at this time .");
Exit ();
}

If (! @ Mysql_select_db ("pms ")){
Echo ("Unable to locate the joke". "database at this time .");
Exit ();
}

Mysql_query ("set names 'gb2312 '");

$ Q = mysql_query ("select * from ability where ALV = 1 ");
While ($ row = mysql_fetch_array ($ q )){
$ Array1 [] = $ row [AName];
}

Array array1 is passed to JavaScript to array ability1

The Code is as follows: Copy code

<Script type = "text/javascript" src = "JS/jquery-1.7.2.min.js"> </script>
<Script type = "text/javascript">
Var ability1 = <? Php echo JSON ($ array1);?>;
Var a = eval ("ability1 ");
Alert (a [0]);
</Script>

Another solution to json Chinese garbled characters


If it is Chinese, pay attention to it.

Find a solution on the Internet:

The Code is as follows: Copy code

 
<? Php
/* Process json_encode Chinese garbled characters */
$ Data = array ('game' => 'Ice-fire status', 'name' => 'thorn spirit ', 'country' => 'Ice-frome status ', 'level' => 45 );
Echo json_encode ($ data );
Echo "<br> ";
$ NewData = array ();
Foreach ($ data as $ key => $ value ){
$ NewData [$ key] = urlencode ($ value );
}
Echo urldecode (json_encode ($ newData ));
?>
 

Later, I consulted someone and I could use base64 encoding. However, base64 encoding cannot be placed in URLs. Baidu explained this as follows:

The standard Base64 is not suitable for direct transmission in the URL, because the URL encoder will convert the "/" and "+" characters in the standard Base64 into the form of "% XX, these "%" signs need to be converted when they are stored in the database, because "%" has been used as a wildcard in ansi SQL.

However, my data is sent through POST, not in the HTTP head, but in the message-body, so it is not affected.

Json_encode can only accept UTF-8 data


For example, if 'signature' is changed to 'u80e5' after json_encode processing, the Chinese part of the final json is replaced with unicode encoding. What we need to solve is to convert the object to json and ensure that the Chinese character inside the object still appears in json as normal Chinese. Now it seems that json_encode alone cannot achieve the goal.
My solution: perform url encoding for the Chinese fields in the class first, then perform json encoding (jsonencode) for the object, and finally url Decoding (urldecode) json, that is, the final json. The Chinese in it is still the Chinese!
The test code is as follows:

 

The Code is as follows: Copy code

<? Php
Class myClass {
Public $ item1 = 1;
Public $ item2 = 'Chinese ';
Function to_json (){
// Url encoding to avoid converting Chinese to unicode using json_encode
$ This-> item2 = urlencode ($ this-> item2 );
$ Str_json = json_encode ($ this );
// Url Decoding. After converting json, all attributes are returned, so that the object attributes remain unchanged.
$ This-> item2 = urldecode ($ this-> item2 );
Return urldecode ($ str_json );
}
}
$ C = new myClass ();
Echo json_encode ($ c );
Echo '<br/> ';
Echo $ c-> to_json ();
Echo '<br/> ';
Echo json_encode ($ c );
Echo '<br/> ';
Echo json_encode ('authorization ');
?>

Program output result:

{"Item1": 1, "item2": "u4e2du6587 "}
{"Item1": 1, "item2": "Chinese "}
{"Item1": 1, "item2": "u4e2du6587 "}
"U80e5"
 


For more information, see: http://www.bKjia. c0m/phper/php/42865.htm

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.