在我的JSON数据中使用单个“智能引号"正在破坏PHP脚本 [英] Using single 'smart quote' in my JSON data is breaking PHP script
问题描述
我有一个PHP脚本正在读取客户端提供的一些JSON数据.提供的JSON数据中只有一个智能引号".
I've got a PHP script that is reading in some JSON data provided by a client. The JSON data provided had a single 'smart quote' in it.
示例:
{
"title" : "Lorem Ipsum’s Dolar"
}
在我的脚本中,我正在使用一个小函数来获取json数据:
In my script I'm using a small function to get the json data:
public function getJson($url) {
$filePath = $url;
$fh = fopen($filePath, 'r') or die();
$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$json = json_decode($temp);
fclose($fh);
return $json;
}
如果我对数据进行utf8编码,则在将其回显时,在引号应该出现的位置看不到任何内容.如果我不对数据进行utf8编码,则在回显数据时会看到有趣的问号符号.
If I utf8 encode the data, when I echo it out I see nothing where the quote should be. If I don't utf8 encode the data, when I echo it out I see the funny question mark symbol �
关于如何真正看到适当角色的任何想法?
Any thoughts on how to actually see the proper character??
谢谢!
推荐答案
服务器是否有可能以类似于Windows-1252的编码发送json数据?该代码页具有一些智能代码字符,其中iso-8859具有控制字符.您能否尝试使用iconv("windows-1252", "utf-8", $temp)
而不是utf8_encode
.如果服务器已经发送了utf-8编码的json,那就更好了,因为这是每个rfc4627推荐的编码.
Is it possibe that the server is sending the json data in an encoding like windows-1252? That codepage has some smart code characters where iso-8859 has control characters. Could you try to use iconv("windows-1252", "utf-8", $temp)
instead of utf8_encode
. Even better would be if the server already sends utf-8 encoded json, since that is the recommended encoding per rfc4627.
这篇关于在我的JSON数据中使用单个“智能引号"正在破坏PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!