PHP json_encode function Alternative (can display in Chinese)

Source: Internet
Author: User
Tags 04x sprintf
  1. /**
  2. * Alternative functions for Json_encode
  3. * Edit bbs.it-home.org
  4. */
  5. function Jsonencode ($var) {
  6. if (function_exists (' Json_encode ')) {
  7. Return Json_encode ($var);
  8. } else {
  9. Switch (GetType ($var)) {
  10. Case ' Boolean ':
  11. Return $var? ' True ': ' false '; lowercase necessary!
  12. Case ' integer ':
  13. Case ' Double ':
  14. return $var;
  15. Case ' resource ':
  16. Case ' string ':
  17. Return ' ". Str_replace (Array ("\ r", "\ n", "<", ">", "&"),
  18. Array (' \ r ', ' \ n ', ' \x3c ', ' \x3e ', ' \x26 '),
  19. Addslashes ($var)). ' ";
  20. Case ' array ':
  21. Arrays in JSON can ' t is associative. If the array is empty or if it
  22. have sequential whole number keys starting with 0, it's not associative
  23. So we can go ahead and convert it as an array.
  24. if (Emptyempty ($var) | | Array_keys ($var) = = = Range (0, sizeof ($var)-1)) {
  25. $output = Array ();
  26. foreach ($var as $v) {
  27. $output [] = Jsonencode ($v);
  28. }
  29. Return ' ['. Implode (', ', $output). '] ';
  30. }
  31. Otherwise, fall through to convert the array as an object.
  32. Case ' object ':
  33. $output = Array ();
  34. foreach ($var as $k = = $v) {
  35. $output [] = Jsonencode (Strval ($k)). ': '. Jsonencode ($v);
  36. }
  37. Return ' {'. Implode (', ', $output). '} ';
  38. Default
  39. return ' null ';
  40. }
  41. }
  42. }
  43. echo jsonencode (Array (' first ' = ' testing ', ' second ' = ' Tangjili '));
  44. ?>
Copy Code

This can also be the code below.

  1. function Php_json_encode ($data) {
  2. if (Is_array ($data) | | is_object ($DATA)) {
  3. $islist = Is_array ($data) && (Emptyempty ($data) | | Array_keys ($data) = = = Range (0,count ($data)-1));
  4. if ($islist) $json = ' ['. Implode (', ', Array_map (' Php_json_encode ', $data)). ']';
  5. else {
  6. $items = Array ();
  7. foreach ($data as $key = = $value) $items [] = Php_json_encode ("$key"). ':' . Php_json_encode ($value);
  8. $json = ' {'. Implode (', ', $items). '}';
  9. }
  10. } elseif (Is_string ($data)) {
  11. $string = ' "'. Addcslashes ($data, "\\\" \n\r\t/". Chr (8). chr (12)). '"';
  12. $json = ";
  13. $len = strlen ($string);
  14. for ($i = 0; $i < $len; $i + +) {
  15. $char = $string [$i];
  16. $c 1 = ord ($char);
  17. if ($c 1 <128) {$json. = ($c 1 >)? $char: sprintf ("\\u%04x", $c 1); continue;}
  18. $c 2 = Ord ($string [+ + $i]);
  19. if (($c 1 &) = = = 0) {$json. = sprintf ("\\u%04x", ($c 1-192) * + $c 2-128); continue;}
  20. $c 3 = Ord ($string [+ + $i]);
  21. if (($c 1 &) = = = 0) {$json. = sprintf ("\\u%04x", (($c 1-224) <<12) + (($c 2-128) << 6) + ($c 3-128) ); Continue }
  22. $c 4 = Ord ($string [+ + $i]);
  23. if (($c 1 & 8) = = = 0) {
  24. $u = (($c 1 &) << 2) + (($c 2>>4) & 3)-1;
  25. $w 1 = (54<<10) + ($u <<6) + (($c 2 &) << 2) + (($c 3>>4) & 3);
  26. $w 2 = (55<<10) + (($c 3 &) <<6) + ($c 4-128);
  27. $json. = sprintf ("\\u%04x\\u%04x", $w 1, $w 2);
  28. }
  29. }
  30. }
  31. else $json = Strtolower (Var_export ($data, true));
  32. return $json;
  33. }
  34. echo php_json_encode (Array (' first ' = ' testing '));
  35. ?>
Copy Code

Chinese words, can be combined with the following function to use.

    1. Function arrayrecursive (& $array, $function, $apply _to_keys_also = False)
    2. {
    3. foreach ($array as $key = = $value) {
    4. if (Is_array ($value)) arrayrecursive ($array [$key], $function, $apply _to_keys_also);
    5. else $array [$key] = $function ($value);
    6. if ($apply _to_keys_also && is_string ($key)) {$new _key = $function ($key), if ($new _key! = $key) {$array [$new _key] = $array [$key]; Unset ($array [$key]); } }
    7. }
    8. }
    9. ?>
Copy Code

Invocation Example:

    1. function JSON ($array) {
    2. Arrayrecursive ($array, ' UrlEncode ', true);
    3. $json = Jsonencode ($array); or $json = Php_json_encode ($array);
    4. Return UrlDecode ($json);
    5. }
    6. Echo JSON (' first ' = ' testing ', ' second ' = ' Chinese ');
    7. ?>
Copy Code
  • 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.