json_encode()抛出错误:参数"无效的UTF-8序列


json_encode() throwing an error: "Invalid UTF-8 sequence in argument"

<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>

它以前可以工作,版本php 5[我相信这是最新的php版本]。

json_encode只允许编码UTF-8字符。看起来像你试图编码的数据可能有非UTF-8字符。

因此,您应该首先将字符串/数据转换为UTF-8,然后再进行编码。

mb_convert_encoding($string,'UTF-8','UTF-8');
json_encode($string);