当前位置:Gxlcms > PHP教程 > GBK页面输出JSON的php代码

GBK页面输出JSON的php代码

时间:2021-07-01 10:21:17 帮助过:20人阅读

  1. function tb_json_encode($value, $options = 0)

  2. {
  3. return json_encode(tb_json_convert_encoding($value, “GBK”, “UTF-8″));
  4. }

  5. function tb_json_decode($str, $assoc = false, $depth = 512)

  6. {
  7. return tb_json_convert_encoding(json_decode($str, $assoc), “UTF-8″, “GBK”);
  8. }

  9. function tb_json_convert_encoding($m, $from, $to)

  10. {
  11. switch(gettype($m)) {
  12. case ‘integer’:
  13. case ‘boolean’:
  14. case ‘float’:
  15. case ‘double’:
  16. case ‘NULL’:
  17. return $m;

  18. case ’string’:

  19. return mb_convert_encoding($m, $to, $from);
  20. case ‘object’:
  21. $vars = array_keys(get_object_vars($m));
  22. foreach($vars as $key) {
  23. $m->$key = tb_json_convert_encoding($m->$key, $from ,$to);
  24. }
  25. return $m;
  26. case ‘array’:
  27. foreach($m as $k => $v) {
  28. $m[tb_json_convert_encoding($k, $from, $to)] = tb_json_convert_encoding($v, $from, $to);
  29. }
  30. return $m;
  31. default:
  32. }
  33. return $m;
  34. }
  35. ?>

人气教程排行