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

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 个字符。

因此,您应该先将string/data转换为UTF-8,然后再进行编码。

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