简体   繁体   中英

accented characters breaks json_encode() in PHP

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 html contains special characters (ie: A méh virága).

How can I get json_encode() not to return 'null' if special accented characters are available?

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.

我的答案是,用utf8_encode()包装html字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM