为什么此对json_encode的PHP调用无声地失败-无法处理单引号? [英] Why is this PHP call to json_encode silently failing - inability to handle single quotes?

查看:85
本文介绍了为什么此对json_encode的PHP调用无声地失败-无法处理单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为$poststdClass对象,当通过print_r()转储时,该对象返回以下内容:

I have a stdClass object called $post that, when dumped via print_r(), returns the following:

stdClass Object (
    [ID] => 12981
    [post_title] => Alumnus' Dinner Coming Soon
    [post_parent] => 0
    [post_date] => 2012-01-31 12:00:51
)

在此对象上调用json_encode()回显结果将导致以下结果:

Echoing the result from calling json_encode() on this object results in the following:

{
    "ID": "12981",
    "post_title": null,
    "post_parent": "0",
    "post_date": "2012-01-31 12:00:51"
}

我假设单引号引起了json_encode的阻塞,但是我不知道需要哪种格式来转义.有什么想法吗?

I'm assuming that something with the single quote is causing json_encode to choke, but I don't know what format is needed to escape that. Any ideas?

修复了代码示例中的不匹配问题.我正在运行PHP版本5.3.8

Fixed mismatch in code examples. I'm running PHP version 5.3.8

在对对象进行编码之后,我立即执行了此操作:

Directly after encoding the object, I did this:

echo json_last_error() == JSON_ERROR_UTF8;

此打印出的1,表示发生以下错误:格式错误的UTF-8字符,可能编码错误". json_last_error()

This printed 1, which means that the following error occurred: "Malformed UTF-8 characters, possibly incorrectly encoded". json_last_error()

在帖子标题上调用utf8_decode()导致以下结果:校友?晚餐即将来临".这些数据是从MySQL数据库中提取的-帖子标题特别是一个文本字段,采用UTF-8编码.也许这个单引号的编码不正确?事实是,我有一个SQL GUI应用程序,它在其中正确显示.

Calling utf8_decode() on the post title resulted in the following: "Alumnus? Dinner Coming Soon". This data is being pulled from a MySQL database - the post title in particular is a text field, UTF-8 encoded. Maybe this single-quote is improperly encoded? The thing is, I have a SQL GUI app, and it appears correctly in that.

推荐答案

在执行查询之前,您需要设置连接编码.如何完成此操作取决于您用于连接的API:

You need to set the connection encoding before executing queries. How this is done depends on the API you are using to connect:

  • call mysql_set_charset("utf8") if you use the old, deprecated API.
  • call mysqli_set_charset("utf8") if you use mysqli
  • add the charset parameter to the connection string if you use PDO and PHP >= 5.3.6. In earlier versions you need to execute SET NAMES utf8.

从MySQL获取数据时,任何文本都将以客户端编码"进行编码,这很可能是 windows-1252 如果不进行其他配置.导致您的问题的字符是弯引号",在十六进制转储中显示为92,它确认mysql客户端正在Windows-1252中对文本进行编码.

When you obtain data from MySQL any text will be encoded in "client encoding", which is likely windows-1252 if you don't configure it otherwise. The character that is causing your problem is the "curly quote", seen as 92 in the hex dump, which confirms that the mysql client is encoding text in windows-1252.

您可能要考虑的另一件事是将所有文本都通过utf8_encode,但是在这种情况下,它不会产生正确的结果. PHP的utf8_encode转换 iso-8859-1 编码的文本.在此编码中,\ x92是不可打印的控制字符,它将在utf-8中转换为不可打印的控制字符.您可以使用str_replace("\x92", "'", $input)来解决此特定字符的问题,但是如果有可能,数据库中将有任何其他非ascii字符,您将希望客户端使用UTF-8.

Another thing you might consider is pass all text through utf8_encode, but in this case it wouldn't produce the correct result. PHP's utf8_encode converts iso-8859-1-encoded text. In this encoding \x92 is a non-printable control character, which would be converted into a non-printable control character in utf-8. You could use str_replace("\x92", "'", $input) to fix the problem for this particular character, but if there's any chance there will be any other non-ascii characters in the database you'll want to have the client use UTF-8.

这篇关于为什么此对json_encode的PHP调用无声地失败-无法处理单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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