json_encode() throwing an error: “Invalid UTF-8 se

2019-08-12 15:04发布

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  json_encode() [<a href='function.json-encode'>function.json-encode</a>]: Invalid UTF-8 sequence in argument</p>
<p>Filename: controllers/share.php</p>
<p>Line Number: 130</p>

It used to work before, version php 5 [which i believe is the latest major PHP version].

标签: php json utf-8
1条回答
一夜七次
2楼-- · 2019-08-12 16:10

json_encode only allows UTF-8 characters to be encoding. Looks like the data you are trying to encode may have non UTF-8 characters.

So, you should first convert the string/data into UTF-8 and then do the encoding.

mb_convert_encoding($string,'UTF-8','UTF-8');
json_encode($string);
查看更多
登录 后发表回答