Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, why do you escape my quotes? [duplicate]

Tags:

json

php

Possible Duplicate:
Why are escape characters being added to the value of the hidden input

So, I have a file called Save.php.

It takes two things: a file, and the new contents.

You use it by sending a request like '/Resources/Save.php?file=/Resources/Data.json&contents={"Hey":"There"}'.

..but of course, encoding the url. :) I left it all unencoded for simplicity and readability.

The file works, but instead of the contents being..

{"Hey":"There"}

..I find..

{\"Hey\":\"There\"}

..which of course throws an error when trying to use JSON.parse when getting the JSON file later through XHR.

To save the contents, I just use..

file_put_contents($url, $contents);

What can I do to get rid of the backslashes?

like image 310
McKayla Avatar asked Jun 12 '11 20:06

McKayla


People also ask

How do you escape characters in PHP?

ESCAPE SEQUENCE IN PHP. As in the introduction above, we have to define the string using double quotes, then start with a backslash followed by a couple of other characters. Here are a few examples of common escape characters in PHP: r Carriage return n New line t Tab " Represents a double quote (in a double-quoted string)

How to escape quotation marks from a string in PHP?

We can use the heredoc syntax <<< to escape quotation marks from a string in PHP. An identifier follows right after the syntax and the string in the new line. The string or the text inside the identifier is called heredoc text. We should use the same identifier after the string in the new line in the first column to denote the end of the heredoc.

What are escape sequences in PHP?

Well, escape sequences are the culprit and answer. In the layman terms, escape sequences are a “special chain” of characters that are used to represent a different meaning. To escape a character in PHP, we have to use double quotes to define the string; Single quotes will take the characters as-they-are.

How to escape special characters within quotes in a string?

Since the string is started with a single quote, all the single quotes inside must be escaped, except the last one. So, the first variable declaration should begin like this : 'From time to "time" this ... theater in \'Hamlet\'... The $text2 variable is actually fine. Backslashes are used in PHP to escape special characters within quotes.


2 Answers

Turn magic_quotes off in PHP.ini.

like image 179
SLaks Avatar answered Oct 08 '22 00:10

SLaks


Looks like you have magic_quotes turned on.

If that is the case, either turn it off - Or use a runtime disabling function

like image 38
Mick Hansen Avatar answered Oct 08 '22 00:10

Mick Hansen