Question

Background: I am not a php programmer, and I only know enough to get the web service parts of my iOS apps working.

I am finding that my php is adding backslashes to my text, when I don't want it to. For example if I set up some variables like this:

$directory = "directory/";
$file = "file.jpg"

then I output via a JSON array back to my device:

$directory . $file 

and I get

"directory\/file.jpg"

I have run get_magic_quotes_gpc with in my php and it reports that magic quotes is turned off. I do this this way:

if (get_magic_quotes_gpc()){
//then I send something back in the JSON array to let me know that get_magic_quotes_gpc is on
}

I have tried to use stripslashes() (on either the original variables or the output) and nothing changes.

So how do I stop it from doing this? As it means that I can't tell my php to delete files in specific directories.

thanks in advance

Was it helpful?

Solution

When you encode your elements do it as so:

json_encode($str, JSON_UNESCAPED_SLASHES);

Using JSON_UNESCAPED_SLASHESshould prevent the addition of unwanted slashes.

OTHER TIPS

If you really, really want to do this, then set the JSON_UNESCAPED_SLASHES option.

json_encode ( $value, JSON_UNESCAPED_SLASHES );

But the slashes do no harm and provide protection against </script> being injected into a <script> element when you inject your JSON into a JavaScript program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top