这次给大家带来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);
}
?>另外从网上搜索到的其他方法
<?php
/*** 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中文网其它相关文章!
推荐阅读:
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
以上就是PHP+JSON输出汉字的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号