PHP array passed to JavaScript and Json_encode GBK _php tutorial for solving garbled language

Source: Internet
Author: User
Tags urlencode
This article describes the PHP array passed to JavaScript and Json_encode GBK Chinese garbled solution, the following is the creation of JSON function, this section from the online one of the heroes
The code is as follows Copy Code

/**************************************************************
*
* Processing of all elements in an array using a specific function
* @param string & $array strings to be processed
* @param string $function the function to execute
* @return Boolean $apply _to_keys_also 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 Array to JSON string (Chinese compatible)
* @param array $array to convert
* JSON string to be converted @return string
* @access Public
*
*************************************************************/
function JSON ($array) {
Arrayrecursive ($array, ' UrlEncode ', true);
$json = Json_encode ($array);
Return UrlDecode ($json);
}

Connect to the database to value the 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 the 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)) {
$array 1[] = $row [AName];
}

Array array1 passed to JavaScript to the array ability1

The code is as follows Copy Code


Another way to solve the problem of JSON Chinese garbled


If it's Chinese, you should pay attention.

Find a workaround on the Web:

The code is as follows Copy Code


/* Handling Json_encode Chinese garbled */
$data = Array (' Game ' = ' ice Fire country ', ' name ' = ' thorn spirit ', ' country ' + ' frost State ', ' level ' = 45);
echo Json_encode ($data);
echo "
";
$newData = Array ();
foreach ($data as $key = = $value) {
$newData [$key] = UrlEncode ($value);
}
Echo UrlDecode (Json_encode ($newData));
?>

Later consulted others, but also can use Base64 code, but base64 code can not put in the URL, Baidu is such explanation:

The standard Base64 is not intended to be transmitted directly in the URL because the URL encoder will change the "/" and "+" characters in the standard Base64 into forms such as "%XX", and these "%" numbers need to be converted when they are deposited into the database because ANSI SQL has used the "%" number as a wildcard character.

But my data is sent via post, not in the head of HTTP, but in Message-body, so it's not affected.

Json_encode can only accept data in utf-8 format


For example: ' Xu ' has been json_encode processed and changed to ' U80e5 ', and the Chinese part of the final JSON is replaced with Unicode encoding. All we have to do is convert the object to JSON and make sure that the Chinese inside the object still appears in the normal Chinese language in JSON, and now it seems that the use of json_encode is not a good idea.
My workaround: First of the class in the text snippet URL encoding (urlencode), and then the object is JSON encoded (Jsonencode), the last URL decoding (urldecode) JSON, that is, the final JSON, the Chinese is still the Chinese!
The test code is as follows:

The code is as follows Copy Code

Class MyClass {
Public $item 1 = 1;
Public $item 2 = ' Chinese ';
function To_json () {
URL encoding to prevent Json_encode from converting Chinese to Unicode
$this->item2 = UrlEncode ($this->item2);
$str _json = Json_encode ($this);
URL decoding, returning each property after the JSON is finished, ensuring that the object properties are not changed
$this->item2 = UrlDecode ($this->item2);
Return UrlDecode ($str _json);
}
}
$c = new MyClass ();
echo Json_encode ($c);
Echo '
';
echo $c->to_json ();
Echo '
';
echo Json_encode ($c);
Echo '
';
Echo json_encode (' Xu ');
?>

Program Output Result:

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


Note With reference to: http://www.bKjia.c0m/phper/php/42865.htm

http://www.bkjia.com/PHPjc/632186.html www.bkjia.com true http://www.bkjia.com/PHPjc/632186.html techarticle This article describes the PHP array passed to JavaScript and Json_encode GBK Chinese garbled solution, the following is the creation of JSON function, this section from the online one of the heroes code as follows copy Generation ...

  • 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.