使用单个“智能引用”;我的 JSON 数据破坏了 PHP 脚本

发布于 2024-09-08 02:09:26 字数 617 浏览 3 评论 0原文

我有一个 PHP 脚本,它正在读取客户端提供的一些 JSON 数据。提供的 JSON 数据中有一个“智能引用”。

示例:

{
    "title"         : "Lorem Ipsum’s Dolar" 
}

在我的脚本中,我使用一个小函数来获取 json 数据:

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 编码,当我回显它时,我会看到有趣的问号符号

� 关于如何实际看到正确的字符有什么想法吗?

谢谢!

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.

Example:

{
    "title"         : "Lorem Ipsum’s Dolar" 
}

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;
}

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??

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窗影残 2024-09-15 02:09:27

问题更多的是生成 JSON 文件。在那里你应该用\'转义'

如果你不能修改这部分,你应该用addslashes这样做:

$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$temp = addslashes($temp);
$json = json_decode($temp);

The issue is more on the side, that generates the JSON file. There you should escape the ' by \'

If you can't modify this part, you should do it like this with addslashes:

$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$temp = addslashes($temp);
$json = json_decode($temp);
茶色山野 2024-09-15 02:09:27

假设数据全部是 utf8,您可以进行字符串替换吗?

$text = str_replace($find, $replace, $text);

寻找下面的角色?

 '“'  // left side double smart quote
 'â€'  // right side double smart quote
 '‘'  // left side single smart quote
 '’'  // right side single smart quote
 '…'  // elipsis
 '—'  // em dash
 '–'  // en dash

Can you possibly do a string replace assuming the data is all utf8?

$text = str_replace($find, $replace, $text);

Looking for the characters below?

 '“'  // left side double smart quote
 'â€'  // right side double smart quote
 '‘'  // left side single smart quote
 '’'  // right side single smart quote
 '…'  // elipsis
 '—'  // em dash
 '–'  // en dash
⊕婉儿 2024-09-15 02:09:26

服务器是否有可能以 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文