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?
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)
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.
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.
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.
Turn magic_quotes
off in PHP.ini.
Looks like you have magic_quotes turned on.
If that is the case, either turn it off - Or use a runtime disabling function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With