json_encode()将非UTF8字符串转换为null,但在直播网站上返回false [英] json_encode() turn non-UTF8 strings into null, but on live site returns false

查看:133
本文介绍了json_encode()将非UTF8字符串转换为null,但在直播网站上返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我必须用于项目(锂)的框架中,特定页面上的输出通过json_encode().在本地和现场我尝试使用一个相同的数据,结果是:

In the framework I have to use for a project (Lithium) the output on a specific page goes through json_encode(). Locally and on live I try with one and the same data, and the result is:

  1. 它在本地返回JSON,但是其中一些值变成了null.
  2. 在线站点-返回false.
  1. Locally it returns the JSON, but some of the values are turned into null.
  2. Live site - it returns false.

在两种情况下,当我运行json_last_error()时-它都会给我int(5).如果某些设置引起了问题,我可以在两个地方都运行phpinfo(). (在本地,我使用PHP 5.3.*,实时版本为5.5.9.)

In both cases when I run json_last_error() - it gives me int(5). I can run phpinfo() on both places, if some setting is causing the problem. (Locally I'm with PHP 5.3.*, on live it's 5.5.9.)

推荐答案

访问 php文档在这里.

尽管此处未在版本日志中记录此问题,但非UTF8处理行为已在5.5中更改,从而使调试变得困难.

Although this is not documented on the version log here, non-UTF8 handling behaviour has changed in 5.5, in a way that can make debugging difficult.

将非UTF-8字符串传递给json_encode()将使该函数在PHP 5.5中返回false,而在以前的版本中,它将仅使此字符串(以及仅此字符串)无效.

Passing a non UTF-8 string to json_encode() will make the function return false in PHP 5.5, while it will only nullify this string (and only this one) in previous versions.

在Latin-1编码的文件中,编写以下代码:

In a Latin-1 encoded file, write this:

$a = array('é', 1);
var_dump(json_encode($a));

PHP< 5.4:string(8) "[null,1]"

PHP < 5.4: string(8) "[null,1]"

PHP> = 5.5:bool(false)

PHP >= 5.5: bool(false)

PHP 5.5当然是正确的(如果编码失败,则返回false),但是它可能会在更新到5.5时引入错误,因为以前即使在一个字符串不在UTF8中时,您也可以获得其余的JSON(如果此字符串)未使用过,您永远都不会通知它已为空)

PHP 5.5 has it right of course (if encoding fails, return false) but its likely to introduce errors when updating to 5.5 because previously you could get the rest of the JSON even when one string was not in UTF8 (if this string wasn't used, you'd never notify it's nulled)

这篇关于json_encode()将非UTF8字符串转换为null,但在直播网站上返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆