Home > Web Front-end > JS Tutorial > body text

PHP+JSON output Chinese characters

php中世界最好的语言
Release: 2018-04-25 11:55:37
Original
1817 people have browsed it

这次给大家带来PHP+JSON输出汉字,PHP+JSON输出汉字的注意事项有哪些,下面就是实战案例,一起来看一下。

解决方法一:

<?php 
function Notice(){
    include &#39;./include/conn.php&#39;;  //数据库链接文件
    $sql_notice = 
mysql_query
(&#39;SELECT * FROM gg_notice where enable = "1" limit 0,10&#39;);
    $notice = 
mysql_fetch_array
($sql_notice, MYSQL_ASSOC);
    $str = json_encode($notice);
    //linux
    return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv(&#39;UCS-2BE&#39;, &#39;UTF-8&#39;, pack(&#39;H4&#39;, &#39;\\1&#39;))", $str);
    //windows
    //return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv(&#39;UCS-2LE&#39;, &#39;UTF-8&#39;, pack(&#39;H4&#39;, &#39;\\1&#39;))", $str);
    
}
?>
Copy after login

另外从网上搜索到的其他方法




 <?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(&#39;UCS-2BE&#39;, &#39;UTF-8&#39;, pack(&#39;H4&#39;, &#39;\\1&#39;))", $json);
        //windows
        //return preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv(&#39;UCS-2LE&#39;, &#39;UTF-8&#39;, pack(&#39;H4&#39;, &#39;\\1&#39;))", $json);
    }
        /**
     * 分析json
     */
    public static function decode($str) {
        return json_decode($str);
    }
}
?>
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

jQuery做出垂直半透明手风琴效果

jquery实现导航菜单鼠标提示功能

The above is the detailed content of PHP+JSON output Chinese characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!