Asked  7 Months ago    Answers:  5   Viewed   37 times

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:


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

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



is there a way to disable it?

Yes, you only need to use the JSON_UNESCAPED_SLASHES flag.

!important read before: (know what you're dealing with - know your enemy)

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. (archived copy).

Example Demo

 * Escaping the reverse-solidus character ("/", slash) is optional in JSON.
 * This can be controlled with the JSON_UNESCAPED_SLASHES flag constant in PHP.
 * @link

$url = '';

echo json_encode($url), "n";

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

Example Output:

Wednesday, March 31, 2021
answered 7 Months ago

What a HORRENDOUS debug session.. well there's good news.. I figured it out..

I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle &quot;, which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('&quot;', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..

Wednesday, March 31, 2021
answered 7 Months ago

I suggest to replace

sed "s/regex/replace/" file


sed "s|regex|replace|" file

if your sed supports it. Then it is no longer necessary to escape the slashes.

The character directly after the s determines which character is the separator, which must appear three times in the s command.

Thursday, June 10, 2021
answered 5 Months ago

Only escape forward slashes when encode_html_chars=True

Check out this-

The JSON spec says forward slashes shall be escaped implicitly.

Here is a solution to do it in JSONEncoder itself. Its just that you create an ESCAPE DICTIONARY and do computation before hand and do the encoding later.

Hope it helps.


Adding to the above solution, there is another reason to escape the characters. As kay said, it gives us some extra sleep. It prevents the attack. So the solution above takes care of all issues.

    # escape all forward slashes to prevent </script> attack
    '/': '\/',
    '\': '\\',
    '"': '\"',
    'b': '\b',
    'f': '\f',
    'n': '\n',
    'r': '\r',
    't': '\t',
Thursday, August 12, 2021
Lawrence Taur
answered 3 Months ago

Debugging suggestion:

Check the output of json_last_error(). It should give you an exact reason why it doesn't work. Available from PHP 5.3.0 only, though.

The reason:

JSONP is not identical with JSON. It contains extra data that breaks json_decode().


Remove the extra brackets using substr($AVDecode, 1, strlen($AVDecode)-2)

Friday, October 22, 2021
answered 1 Day ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :