【问题标题】:json_encode() throwing an error: "Invalid UTF-8 sequence in argument"json_encode() 抛出错误:“参数中的 UTF-8 序列无效”
【发布时间】:2015-05-21 02:41:40
【问题描述】:
<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 版本]。

【问题讨论】:

  • 被解码的字符串是什么
  • 使用echo bin2hex(...)显示“无效”的东西,以便我们帮助您解决。

标签: php json utf-8


【解决方案1】:

json_encode 只允许对UTF-8 字符进行编码。看起来您尝试编码的数据可能包含非 UTF-8 字符。

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

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

【讨论】:

  • "mb_convert_encoding() 期望参数 1 为字符串,给定数组"
  • 我猜这个错误是不言自明的。您需要发送一个字符串进行转换。不是数组。
  • 但我需要通过阵列才能让我的网站正常运行。有没有办法转换数组?
  • 将所有数组元素转换为 UTF-8?将foreachmb_convert_encoding 一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多