Home >Backend Development >PHP Tutorial >Solve the problem of php json_encode Chinese transcoding and garbled characters

Solve the problem of php json_encode Chinese transcoding and garbled characters

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:13:201256browse

Example, php json_encode Chinese transcoding and garbled code problems.

  1. // Prevent json Chinese transcoding

  2. function jsonEncodeWithCN($data) {
  3. return preg_replace("/\u([0-9a-f]{4} )/ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '$1'))", json_encode($data));
  4. }

  5. < p>$arr = array('a'=>'test', b=>'hello');

  6. echo jsonEncodeWithCN($arr);

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