PHP JSON格式的中文显示问题解决方法
返回json数据中文显示的问题
上一篇文章中,返回json格式的中文显示成\u5723\u8bde\u8282\u5343\u4e07\u597d\u793c\u5927\u5949\u9001
解决方法一:
<?php
functionNotice(){
include'./include/conn.php'; //数据库链接文件
$sql_notice=mysql_query('SELECT*FROMgg_noticewhereenable="1"limit0,10');
$notice=mysql_fetch_array($sql_notice,MYSQL_ASSOC);
$str=json_encode($notice);
//linux
returnpreg_replace("#\\\u([0-9a-f]{4})#ie","iconv('UCS-2BE','UTF-8',pack('H4','\\1'))",$str);
//windows
//returnpreg_replace("#\\\u([0-9a-f]{4})#ie","iconv('UCS-2LE','UTF-8',pack('H4','\\1'))",$str);
}
?>
另外从网上搜索到的其他方法
<?php
/**
*json生成,分析支持中文
*/
classJson_Helper{
/**
*生成json
*/
publicstaticfunctionencode($str){
$json=json_encode($str);
//linux
returnpreg_replace("#\\\u([0-9a-f]{4})#ie","iconv('UCS-2BE','UTF-8',pack('H4','\\1'))",$json);
//windows
//returnpreg_replace("#\\\u([0-9a-f]{4})#ie","iconv('UCS-2LE','UTF-8',pack('H4','\\1'))",$json);
}
/** *分析json */ publicstaticfunctiondecode($str){ returnjson_decode($str); } } ?>