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

2019-08-12 15:42发布

问题:

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

回答1:

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


标签: php json utf-8