欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php json_encode函数的替代方法(可显示中文)

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

中文的话,可以结合如下的函数来使用。

  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. ?>
复制代码

调用示例:

  1. function JSON($array) {
  2. arrayRecursive($array, 'urlencode', true);
  3. $json = jsonEncode($array); // 或者 $json = php_json_encode($array);
  4. return urldecode($json);
  5. }
  6. echo JSON(array('first'=>'testing','second'=>'中文'));
  7. ?>
复制代码