How to keep json_encode() from dropping strings with invalid characters

后端 未结 6 711
醉话见心
醉话见心 2020-11-29 06:24

Is there a way to keep json_encode() from returning null for a string that contains an invalid (non-UTF-8) character?

It can be a pain in t

6条回答
  •  伪装坚强ぢ
    2020-11-29 06:52

    Instead of using the iconv function, you can direclty use the json_encode with the JSON_UNESCAPED_UNICODE option ( >= PHP5.4.0 )

    Make sure you put "charset=utf-8" in the header of your php file:

    header('Content-Type: application/json; charset=utf-8');

提交回复
热议问题