json_encode() escaping forward slashes
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?
Yes, you only need to use the
JSON_UNESCAPED_SLASHES
flag.json_encode($str, JSON_UNESCAPED_SLASHES);
Example Demo
$data = array( "app_name" => env('APP_NAME'), "queue" => 'update-order', "payload" => [ "entity_id" => $entity_id, "order_id" => $request['order_id'], "delivery_date" => $orderDate[0]->order_delivery_date, "template_id" => $templateId->id, "endpoint" => env('APP_URL') ],);$response = json_encode($data, JSON_UNESCAPED_SLASHES);
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/"