PHP5.3 htmlentities与json_encode的坑

2023-05-28,,

今天在线上使用了

json_encode(htmlentities($value))

发现出现了!

[03-Jul-2015 02:52:44 UTC] PHP Warning:  json_encode_plus() [<a href='function.json-encode-plus'>function.json-encode-plus</a>]: Invalid UTF-8 sequence in argument in /home/users/ouerqiang/fbiz/framework/BizBootstrap.php on line 106

乍一看,不知道出现了什么问题,经过看来资料发现PHP 5.4之前的

htmlentities(string,quotestyle,character-set)

其中character-set=SO-8859-1。

如果里面有中文的话就就乱码了不能转义了,json_encode只认UTF-8.导致出现报错。

改为

htmlentities($instance,ENT_QUOTES ,'UTF-8')

解决

《PHP5.3 htmlentities与json_encode的坑.doc》

下载本文的Word格式文档,以方便收藏与打印。