編碼問題產生:
demo.php
內容如下:
<?php
$text_arr = ['text'=>"一切皆往事"];
file_put_contents('test.txt', json_encode($text_arr, true));
運行demo.php
后生成test.txt
內容如下:
{"text":"\u4e00\u5207\u7686\u5f80\u4e8b"}
解決上面的編碼問題:
demo1.php
內容如下:
<?php
$text_arr = ['text'=>"一切皆往事"];
file_put_contents('test.txt', json_encode($text_arr, JSON_UNESCAPED_UNICODE));
運行demo1.php
后產生test.txt
內容如下:
{"text":"一切皆往事"}
注意事項:JSON_UNESCAPED_UNICODE
使用時要求php >= 5.4
。