json_encode()抛出一个错误:“在参数中无效的UTF-8序列”

时间:2022-03-01 10:36:15
<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 5[我认为是最新的主要php版本]。

1 个解决方案

#1


4  

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.

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

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

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

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

#1


4  

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.

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

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

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

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