0

I am trying to add 2 Strings like this:

$response = array(); 

$target_dir = "uploads/";
$public_key = "f1vlje6378uh6ucok8sda1exo5lmbu"; 
$target_dir .= $public_key."/";

$response["target_dir created"] = $target_dir; 
echo json_encode($response); 

"target_dir created": "uploads\/f1vlje6378uh6ucok8sda1exo5lmbu\/"

Any ideas why this "\/" appear and not just "/" ?

stavros.3p
  • 2,244
  • 6
  • 20
  • 37
  • 3
    See: https://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped – Progrock Feb 23 '20 at 15:37
  • 1
    a backslash is used to escape the slash. There is no problem with that. If you parse the JSON back to an array, it will be in normal form, without the backslash. – OMi Shah Feb 23 '20 at 15:41

1 Answers1

1

It's the default behavior where it escapes the slash.

If you do not want to get the slashes to be escaped, you can do it like this:

echo json_encode($response, JSON_UNESCAPED_SLASHES);

Here's the documentation of json_encode() and the various options you can use: https://www.php.net/manual/en/function.json-encode.php

Akhilesh B Chandran
  • 6,523
  • 7
  • 28
  • 55