Home  >  Article  >  Backend Development  >  Alternative method to php json_encode function (can display Chinese)

Alternative method to php json_encode function (can display Chinese)

WBOY
WBOYOriginal
2016-07-25 08:59:42924browse
  1. /**
  2. * Alternative function 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 be associative. If the array is empty or if it
  22. // has 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. ?>
复制代码

也可以是下面这样的代码。

  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, "\"nrt/" . chr(8) . chr(12)) . '"';
  12. $json = '';
  13. $len = strlen($string);
  14. for( $i = 0; $i < $len; $i++ ) {
  15. $char = $string[$i];
  16. $c1 = ord($char);
  17. if( $c1 <128 ) { $json .= ($c1 > 31) ? $char : sprintf("\u%04x", $c1); continue; }
  18. $c2 = ord($string[++$i]);
  19. if ( ($c1 & 32) === 0 ) { $json .= sprintf("\u%04x", ($c1 - 192) * 64 + $c2 - 128); continue; }
  20. $c3 = ord($string[++$i]);
  21. if( ($c1 & 16) === 0 ) { $json .= sprintf("\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128)); continue; }
  22. $c4 = ord($string[++$i]);
  23. if( ($c1 & 8 ) === 0 ) {
  24. $u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1;
  25. $w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3);
  26. $w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128);
  27. $json .= sprintf("\u%04x\u%04x", $w1, $w2);
  28. }
  29. }
  30. }
  31. else $json = strtolower(var_export( $data, true ));
  32. return $json;
  33. }
  34. echo php_json_encode(array('first'=>'testing'));
  35. ?>
复制代码

For Chinese, it can be used in combination with the following functions.

  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

Call 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(array('first'=>'testing','second'=>'Chinese'));
  7. ?>
Copy Code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn