在PHP的json_encode截断之前转义JSON撇号? [英] Escaping JSON apostrophe before PHP's json_encode truncates?

查看:125
本文介绍了在PHP的json_encode截断之前转义JSON撇号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL数据库(utf8_general_ci)中有一个字段,该字段包含一个大写的撇号:Owner’s...

I have a field in a MySQL database (utf8_general_ci) that contains a curly (smart?) apostrophe: Owner’s...

如果我访问将其从数据库中拉出的PHP页面,则此打印效果很好,没有任何特殊处理.但是,我试图通过另一个页面上的$ .getJSON请求访问它,所以我使用了PHP的json_encode.它会截断该值,以便读取Owner,然后成功编码其余数据.如果我在json_encode之前在字段上使用PHP的utf8_encode,则它包含完整的值,其中编码为\u0092,然后在页面上不打印任何内容,给我Owners. PHP的htmlentitieshtmlspecialchars无效.

This prints fine with no special handling if I access the PHP page that pulls it from the DB. However, I am trying to access it via a $.getJSON request on another page, so I used PHP's json_encode. It truncates the value so that it reads Owner, then successfully encodes the rest of the data. If I use PHP's utf8_encode on the field before I json_encode, it includes the full value with the encoded to \u0092 which then doesn't print anything on the page, giving me Owners. PHP's htmlentities and htmlspecialchars have no effect.

查看Chrome工具中的请求,Owner’s在$ .getJSON页面上显示为Owner�s.

Looking at the request in Chrome's tools, Owner’s is shown as Owner�s on the $.getJSON page.

有人可以在这里帮助我吗?我已经在SO和Web上阅读了其他问题,但找不到任何有用的东西,并且在JSON方面工作不多.

Can anyone help me out here? I have read other questions on SO and the web but I cannot find anything that helps and I haven't worked much with JSON.

感谢阅读.

推荐答案

在我的json_encode()之前使用PHP的utf8_encode()确实确实阻止了之后的数据被切断,但是它也将其编码为\0092没有显示(控制字符).当我在查询之前使用MySQL的SET NAMES utf8时,我根本不必使用utf8_encode(),并且我的json是通过映射到\u2019的方式正确编码的,显示效果很好.

Using PHP's utf8_encode() before my json_encode() did indeed stop the data from cutting off after the but it also encoded it to \0092 which did not display (control character). When I used MySQL's SET NAMES utf8 before my query, I did not have to use utf8_encode() at all, and my json was encoded correctly with mapping to \u2019, which displays nicely.

感谢@Pekka的链接,它帮助我缩小了可能性.

Thanks for the link @Pekka, it helped me narrow down the possibilities.

这篇关于在PHP的json_encode截断之前转义JSON撇号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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