1

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..

2
  • That is the way json prints. you can split or explode the string and the work on that. Commented Jan 13, 2014 at 5:39
  • Try again json_decode then print the array and see result. Commented Jan 13, 2014 at 6:35

4 Answers 4

6

Use JSON_UNESCAPED_SLASHES It will work

echo json_encode($resultArray, JSON_UNESCAPED_SLASHES);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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'];

Comments

0

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);
?>

6 Comments

Thanks for your help... rawurlencode didn't resolve my issue.. It echos the same result which i'm facing.. Anyways... "stripslashes" helped me to resolve this issue.. Thanks for helping me..
@AJ101 Ok I didnt say encode i said decode. lol What I will do is update the answer so you can mark my updated answer as correct so that other people can see it and so that it can help them if they run across the same problem
Pavan, Now you have updated the your comment with this "stripslashes" function. Which i already got the answer from other guy. Also, lol i'm not going to give any mark to your updated comment.. Thanks for your time..
@AJ101 No problem dude. I simply updated my answer so that others could benefit and also so that my resource would include the most up to date information. If you arent happy with my answer please do mark the "other" guy's answer as correct, whos name btw is esqew. Have some respect to the person atleast who helped you. Do everyone a favour and scroll up to esqew's answer and tick it as correct already so that other users coming to your question can see the correct answer right away, and so that you can give some points to the user as a token of your gratitiude. Goodluck man.
Buddy... i do give the respect to the person who helped me ... You don't have to tell me that and you are not my boss or something to tell me what i have to do and what not. I gave respect to you as well.. As you suggest, i gave esqew's answer as a tick. Try next time Buddy...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.