php汉字转码示例

发布时间:2020-08-16编辑:脚本学堂
本文介绍了php汉字转码的一个例子,php结合json实现汉字转码,有需要的朋友参考下。

1,json文件
 

复制代码 代码示例:
querys.json:
   {"cities":["u4e0au6d77","u4e1cu839e","u4e2du5c71","u4e3du6c34","u4e4cu9c81u6728u9f50","u4f5bu5c71","u4fddu5b9a","u5170u5dde","u5317u4eac","u5357u4eac","u5357u5b81","u5357u660c","u5357u901a","u8d35u9633","u8fdeu4e91u6e2f","u90a2u53f0","u90afu90f8","u90d1u5dde","u91cdu5e86","u91d1u534e","u94f6u5ddd","u9547u6c5f","u957fu6625","u957fu6c99","u9752u5c9b"]}

2,php汉字转码文件 test.php
 

复制代码 代码示例:

<? php
header("content-Type: text/html; charset=Utf-8");
set_time_limit(0);  //设定一个程式所允许执行的秒数,0为最大限度

$file = "querys.json";
$content = file_get_contents($file);
echo $content."<br/>";

function replace_unicode_escape_sequence($match){    
   return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
$str = preg_replace_callback('/\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence',   $content);
echo $str;
?>