accented characters breaks json_encode() in PHP

后端 未结 2 573
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 10:28

I have an array which one of the keys contains some HTML code. I am json_encoding this array for returning. I have recently noticed that json_encode() returns \'null\' if the ht

相关标签:
2条回答
  • 2021-01-21 11:15

    json_encode() assumes that text is in UTF-8 encoding. If the input looks like malformed UTF-8, it returns null. The only way to get json_encode() to work is to give it input in UTF-8 (which you should probably be using anyway).

    And, as Zathrus Writer says in a comment, the actual PHP source code itself should probably also be in UTF-8 encoding, to prevent subtle bugs.

    0 讨论(0)
  • 2021-01-21 11:15

    My answer, wrapped the html string with utf8_encode().

    0 讨论(0)
提交回复
热议问题