GBK页面输出JSON的php代码

原创
2016-07-25 09:02:46 687浏览
  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. ?>
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。