Question

I have 1 key-value array in PHP. For Example:

<?php
$url = "https://mywebsite.com";
$resultArray = array(
    "error" => "null",
    "url" => $url,
);
echo json_encode($resultArray);
?>

**Output:** {"error":"null","url":"https:\/\/mywebsite.com"}

As you see above when I echo the json encode output. The browser itself add backslash "\" with each trailing slash "/". Means if i pass "https://mywebsite.com" in json key-value pair the result of i got is something like this "https://mywebsite.com" . But i don't want that backslash. I wants to echo a plain URL.. I used the urlencode method but that converts the slashes to encode format.. How can i resolve this issue.. I searched many topics on net but i didnt get the exact output which i'm looking at..

Was it helpful?

Solution 2

It looks to me like Magic Quotes is on. Use stripslashes or turn off Magic Quotes.

OTHER TIPS

Use JSON_UNESCAPED_SLASHES It will work

echo json_encode($resultArray, JSON_UNESCAPED_SLASHES);

You are right, nothing wrong with your URL. You are getting right output. When you again decode this json data you will get original output.

$json_data = json_encode($resultArray);
$obj = json_decode($json_data, true);
echo $output_rul = $obj['url'];

You can test out the built in php functions stripslashes or rawurldecode.

<?php
$url = "https://mywebsite.com";
$resultArray = array(
    "error" => "null",
    "url" => stripslashes($url), //or try this rawurldecode($url)
);
echo json_encode($resultArray);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top