php json_encode Chinese encoding problem

WBOY
Release: 2016-07-25 09:04:22
Original
1063 people have browsed it
  1. $a = array('city' => "Beijing"'abcdTianjin");
  2. echo json_encode($a) . "n";
  3. ?>
  4. debian-test -server:/home/php# php test1.php
  5. {"city":"u5317u4eac"'\abcdu5929u6d25"}
Copy code

Requirements, a field in the database can save multiple values, so you need to The data is encoded in json and stored in the database. After processing with the built-in json_encode function of PHP, the Chinese becomes unicode code (such as {"city":"u5317u4eac"'\abcdu5929u6d25"}). Although the web page can process it correctly, but from The data synchronized from the mobile phone is Chinese characters (such as {"city":"Beijing"'\abcdTianjin"}), not unicode. In order to store the data passed from two places in the database with the same encoding, we will consider it for now. Convert unicode codes to Chinese characters or customize a json_encode function. This function will not convert Chinese characters into unicode codes.

I found a function on the official PHP website that converts data to json, and Chinese will not be converted into unicode.

  1. /**

  2. * Since the function json_encode that comes with PHP's json extension will convert Chinese characters into unicode codes
  3. * So we use the custom json_encode here. This function will not convert Chinese characters into unicode codes
  4. */
  5. function customJsonEncode($a = false) {
  6. if (is_null($a)) return 'null';
  7. if ($a === false) return 'false';
  8. if ($a === true) return 'true';
  9. if (is_scalar($a)) {
  10. if (is_float($a)) {
  11. / / Always use "." for floats.
  12. return floatval(str_replace(",", ".", strval($a)));
  13. }

  14. if (is_string($a) ) {

  15. static $jsonReplaces = array(array("\", "/", "n", "t", "r", "b", "f", '"'), array('\\' , '\/', '\n', '\t', '\r', '\b', '\f', '"'));
  16. return '"' . str_replace($jsonReplaces[0] , $jsonReplaces[1], $a) . '"';
  17. } else {
  18. return $a;
  19. }
  20. }

  21. $isList = true;

  22. for ($i = 0 , reset($a); $i < count($a); $i++, next($a)) {
  23. if (key($a) !== $i) {
  24. $isList = false;
  25. break ;
  26. }
  27. }

  28. $result = array();

  29. if ($isList) {
  30. foreach ($a as $v) $result[] = customJsonEncode($v);
  31. return '[' . join(',', $result) . ']';
  32. } else {
  33. foreach ($a as $k => $v) $result[] = customJsonEncode($k).': '.customJsonEncode($v);
  34. return '{' . join(',', $result) . '}';
  35. }
  36. }

  37. $a = array('a' => array('c' => 'China\"'Country', 'd' => 'South Korea'), 'b' => 'Japan');

  38. echo customJsonEncode($a) . l ;
  39. $b = array(array('c' => 'China\"'Country', 'd' => 'South Korea'), 'Japan');
  40. echo customJsonEncode($b) . l;
  41. ?>

Copy code

output: {"a":{"c":"China\"'country","d":"South Korea"},"b":"Japan"} [{"c":"China\"'country","d":"South Korea"},"Japan"]



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template