php 解決json_encode中文UNICODE轉碼問題

來源:互聯網
上載者:User

標籤:

用PHP的json_encode來處理中文的時候, 中文都會被編碼, 變成不可讀的, 類似”\u***”的格式,如果想漢字不進行轉碼,這裡提供三種方法

 

1.升級PHP,在PHP5.4, 這個問題終於得以解決, Json新增了一個選項: JSON_UNESCAPED_UNICODE, 故名思議, 就是說, Json不要編碼Unicode.

<?phpecho json_encode("中文", JSON_UNESCAPED_UNICODE);//"中文"

2.把漢字先urlencode然後再使用json_encode,json_encode之後再次使用urldecode來解碼,這樣編碼出來的json數組中的漢字就不會出現unicode編碼了。

$array = array(‘test‘=>urlencode("我是測試"));$array = json_encode($array);echo urldecode($array);//{"test":"我是測試"} 

3.對unicode碼再進行解碼,解碼函數如下:

function decodeUnicode($str){    return preg_replace_callback(‘/\\\\u([0-9a-f]{4})/i‘,        create_function(            ‘$matches‘,            ‘return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");‘        ),        $str);}4.例子$arr = array(‘name1‘:"中文",‘name2‘:‘abc12‘);$jsonstr = decodeUnicode(json_encode($arr)); 

http://www.cnblogs.com/sink_cup/archive/2011/05/28/php_json_encode_unicode_decode.html

http://www.veryhuo.com/a/view/35112.html

http://www.alixixi.com/program/a/2011112776664.shtml

php 解決json_encode中文UNICODE轉碼問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.