这次给大家带来PHP+JSON输出汉字,PHP+JSON输出汉字的注意事项有哪些,下面就是实战案例,一起来看一下。
解决方法一:<?php
function Notice(){
include './include/conn.php'; //数据库链接文件
$sql_notice =
mysql_query
('SELECT * FROM gg_notice where enable = "1" limit 0,10');
$notice =
mysql_fetch_array
($sql_notice, MYSQL_ASSOC);
$str = json_encode($notice);
//linux
return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $str);
//windows
//return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2LE', 'UTF-8', pack('H4', '\\1'))", $str);
}
?>
另外从网上搜索到的其他方法
/*** json 生成,分析 支持中文
*/
class Json_Helper {
/**
* 生成json
*/
public static function encode($str){
$json = json_encode($str);
//linux
return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $json);
//windows
//return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2LE', 'UTF-8', pack('H4', '\\1'))", $json);
}
/**
* 分析json
*/
public static function decode($str) {
return json_decode($str);
}
}
?>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读: