229

I'm pulling JSON from Instagram:

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

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

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

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

1
  • 2
    It is optional: "the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F). Any character may be escaped." Commented Apr 18, 2012 at 13:29

4 Answers 4

397
Answer recommended by PHP Collective

is there a way to disable it?

Yes, you only need to use the JSON_UNESCAPED_SLASHES flag (PHP 5.4+).

!important read before: https://stackoverflow.com/a/10210367/367456 (know what you're dealing with - know your enemy: DO NOT USE in web/html context - CLI, unless CGI, might be fine thought, if they think they need it in JSON HTTP context for readability purposes, they have a different problem)

json_encode($str, JSON_UNESCAPED_SLASHES); 

If you don't have PHP 5.4 at hand (you certainly already asserted the warning above), pick one of the many existing functions and modify them to your needs, e.g. 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/" 
Sign up to request clarification or add additional context in comments.

11 Comments

This answer is grate but JSON Encode as standard you should leave it as escaped and then in the reciving end undo the escaping php's strip_slashes and for JS phpjs.org/functions/stripslashes
@MartinBarker: This might have been a problem only with PHP 5.2.1: 3v4l.org/0AahO - The JSON is valid in both cases.
Ugly PHP! the JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES must be default, not (strange) option... How to say it to php-developers?
Thanks @hakre! Well, it is a manifest :-) stackoverflow.com/a/45681613/287948
@MikkoRantalainen: Yes, appplies to both as JSON's own encoding is only UTF-8. The devil is in the detail in two scopes: HTML and Javascript. Considering the audience of this site, and given that most consuming users have still problems to figure out which encoding it is (if not being confronted with one for the first time), it may not be necessary, but it can be safely ignored. There was a quirk in the defaults in some PHP version regarding this, but its fixed nowadays.
|
59

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.

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

9 Comments

So will javascript automatically remove the backslashes when it pulls in the json, or this something I need to specify?
It will automatically handle it. Like in many other languages, escaping characters without a special meaning in the language just leaves those characters alone.
@MichaelC. In JSON the string value "\/" is exactly the same as string value "/" - and a working JSON parser will treat it as such. The same rule applies to javascript strings, so feeding JSON as a javascript code will not have any troubles either. I am surprised PHP even has the JSON_UNESCAPED_SLASHES flag.
But what if one is encoding the data into JSON and then storing them into a database column? If the program has some mechanism to escape characters for database's sake, wouldn't this feature be very inconvenient since now the slashes are being double-escaped?
Dumping JSON into script tags without an encoding step on the assumption that the JSON implementation was escaping slashes seems like a practice that is certain to eventually bite you.
|
4

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 -

my expected:

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

would be encoded to:

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

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

addcslashes($url, '/')

allowed for the proper output during my comparisons.

Comments

-4

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

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

did work for me.

2 Comments

This is NOT correct, str_replace("\\/","/",$variable) or str_replace('\/','/',$variable)
@a55 Even with a single quoted string, you still have to escape backslashes: str_replace('\\/', '/', $v);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.