PHP實現json_decode不轉義中文的方法詳解

來源:互聯網
上載者:User
這篇文章主要介紹了PHP實現json_decode不轉義中文的方法,結合執行個體形式具體分析了php5.4+及5.3版本針對json_decode實現不轉義中文的具體操作技巧與相關注意事項,需要的朋友可以參考下

具體如下:

預設情況下PHP的 json_decode 方法會把特殊字元進行轉義,還會把中文轉為Unicode編碼形式。

這使得資料庫查看文本變得很麻煩。所以我們需要限制對於中文的轉義。

對於PHP5.4+版本,json_decode函數第二個參數,可以用來限制轉義範圍。

要限制中文,使用JSON_UNESCAPED_UNICODE參數。

json_encode($a, JSON_UNESCAPED_UNICODE);

對於PHP5.3版本,可以先把ASCII 127以上的字元轉換為HTML數值,這樣避免被json_decode函數轉碼:

function my_json_encode($arr){    //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding    array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); });    return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');}

相關推薦:

php實現app介面並返回json資料的方法

getJSON()非同步請求伺服器返回json格式資料(圖文教程)

JS中JSON對象定義與取值實現步驟詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.