php json_encode() 转义正斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10210338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:40:14  来源:igfitidea点击:

json_encode() escaping forward slashes

phpjson

提问by Michael C.

I'm pulling JSON from Instagram:

我正在从 Instagram 中提取 JSON:

$instagrams = json_decode($response)->data;

$instagrams = json_decode($response)->data;

Then parsing variables into a PHP array to restructure the data, then re-encoding and caching the file:

然后将变量解析为 PHP 数组以重构数据,然后重新编码和缓存文件:

file_put_contents($cache,json_encode($results));

file_put_contents($cache,json_encode($results));

When I open the cache file all my forward slashes "/" are being escaped:

当我打开缓存文件时,我所有的正斜杠“/”都被转义了:

http:\/\/distilleryimage4.instagram.com\/410e7...

http:\/\/distilleryimage4.instagram.com\/410e7...

I gather from my searches that json_encode()automatically does this...is there a way to disable it?

我从我的搜索中收集json_encode()自动执行此操作...有没有办法禁用它?

回答by hakre

is there a way to disable it?

有没有办法禁用它?

Yes, you only need to use the JSON_UNESCAPED_SLASHESflag.

是的,您只需要使用JSON_UNESCAPED_SLASHES标志。

!importantread before: https://stackoverflow.com/a/10210367/367456(know what you're dealing with - know your enemy)

!important之前阅读:https: //stackoverflow.com/a/10210367/367456(知道你在处理什么 - 了解你的敌人)

json_encode($str, JSON_UNESCAPED_SLASHES);

If you don't have PHP 5.4 at hand, pick one of the many existing functions and modify them to your needs, e.g. http://snippets.dzone.com/posts/show/7487 (archived copy).

如果您手头没有 PHP 5.4,请选择众多现有功能之一并根据您的需要修改它们,例如http://snippets.dzone.com/posts/show/7487 (archived copy)

Example Demo

示例演示

<?php
/*
 * Escaping the reverse-solidus character ("/", slash) is optional in JSON.
 *
 * This can be controlled with the JSON_UNESCAPED_SLASHES flag constant in PHP.
 *
 * @link http://stackoverflow.com/a/10210433/367456
 */    

$url = 'http://www.example.com/';

echo json_encode($url), "\n";

echo json_encode($url, JSON_UNESCAPED_SLASHES), "\n";

Example Output:

示例输出:

"http:\/\/www.example.com\/"
"http://www.example.com/"

回答by ThiefMaster

Yes, but don't - escaping forward slashes is a good thing. When using JSON inside <script>tags it's necessary as a </script>anywhere - even inside a string - will end the script tag.

是的,但不要逃避正斜杠是一件好事。在<script>标签内使用 JSON时,有必要在</script>任何地方 - 甚至在字符串内 - 将结束脚本标签。

Depending on where the JSON is used it's not necessary, but it can be safely ignored.

根据使用 JSON 的位置,它不是必需的,但可以安全地忽略它。

回答by Squadrons

On the flip side, I was having an issue with PHPUNIT asserting urls was contained in or equal to a url that was json_encoded -

另一方面,我遇到了 PHPUNIT 断言 url 包含在或等于 json_encoded 的 url 中的问题 -

my expected:

我的预期:

http://localhost/api/v1/admin/logs/testLog.log

http://localhost/api/v1/admin/logs/testLog.log

would be encoded to:

将被编码为:

http:\/\/localhost\/api\/v1\/admin\/logs\/testLog.log

http:\/\/localhost\/api\/v1\/admin\/logs\/testLog.log

If you need to do a comparison, transforming the url using:

如果您需要进行比较,请使用以下方法转换 url:

addcslashes($url, '/')

addcslashes($url, '/')

allowed for the proper output during my comparisons.

在我的比较过程中允许正确的输出。

回答by Anton De Rose

I had to encounter a situation as such, and simply, the

我不得不遇到这样的情况,简单地说,

str_replace("\/","/",$variable)

did work for me.

确实为我工作。