JSON编码和弯引号 [英] JSON Encode and curly quotes

查看:141
本文介绍了JSON编码和弯引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

json_encode()的本机PHP 5实现中,我遇到了一个有趣的行为.显然,在将对象序列化为json字符串时,编码器将使包含卷曲"引号的字符串中的所有属性为空,这种属性可能会在启用自动转换的情况下从MS Word文档中复制粘贴.

I've run into an interesting behavior in the native PHP 5 implementation of json_encode(). Apparently when serializing an object to a json string, the encoder will null out any properties that are strings containing "curly" quotes, the kind that would potentially be copy-pasted out of MS Word documents with the auto conversion enabled.

这是该功能的预期行为吗?我该怎么做才能迫使这些字符隐蔽到它们的基本等同物上?我已经检查了返回数据的数据库和插入的管理页面之间的字符编码不匹配,并且一切都正确设置了-看起来好像编码器由于这些字符而拒绝了这些值.还有其他人遇到过这种行为吗?

Is this an expected behavior of the function? What can I do to force these kinds of characters to covert to their basic equivalents? I've checked for character encoding mismatches between the database returning the data and the administration page the inserts it and everything is setup correctly - it definitely seems like the encoder just refuses these values because of these characters. Has anyone else encountered this behavior?

要澄清;

MSWord将使用标准的引号和撇号,并将其转换为更美观的花哨"或卷曲"引号.将这些字符放在内容管理器中时,这些字符在其编辑界面(在html中)和数据库编码之间的字符集不匹配,会引起问题.

MSWord will take standard quotation marks and apostraphes and convert them to more aesthetic "fancy" or "curly" quotes. These characters can cause problems when placed in content managers that have charset mistmatches between their editing interface (in the html) and the database encoding.

但是,这不是问题所在.例如,我有一个代表人的个人资料的json_object和字符串:

That's not the problem here, though. For example, I have a json_object representing a person's profile and the string:

Jim O’Shea

该单引号的UTF代码为\u2019

The UTF code for that apostraphe being \u2019

从数据库中获取并直接进行json_encoded时,json对象中的结果将为null.

Will come out null in the json object when fetched from database and directly json_encoded.

{"model_name":"Bio","logged":true,"BioID":"17","Name":null,"Body":"Profile stuff!","Image":"","Timestamp":"2011-09-23 11:15:24","CategoryID":"1"}

推荐答案

从未遇到过此特定问题(即json_encode()),但是我在其他地方使用的一种简单(尽管有点难看)的解决方案是遍历您的数据并通过我从某处获得的此功能将其传递(当我找到它的来源时会记入它):

Never had this specific problem (i.e. with json_encode()) but a simple - albeit a bit ugly - solution I have used in other places is to loop through your data and pass it through this function I got from somewhere (will credit it when I find out where I got it):

function convert_fancy_quotes ($str) {
  return str_replace(array(chr(145),chr(146),chr(147),chr(148),chr(151)),array("'","'",'"','"','-'),$str);
}

这篇关于JSON编码和弯引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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