Php escape double quotes only
I am generating JSON with PHP. Show I have been using
outputs: Perfectly valid JSON Unfortunately addslashes also escapes single quotes with catastrophic results for valid JSON
outputs: In short, is there a way to only escape double quotes?
Erik B 39.2k24 gold badges115 silver badges127 bronze badges asked Apr 10, 2011 at 11:33
1 Although you should use
You could also use a regular expression based replacement:
answered Apr 10, 2011 at 11:42
GumboGumbo 628k106 gold badges767 silver badges838 bronze badges 0
There is no function like However you can make use of
That does exactly as described. If you want to have it compatible with
That should do the job you've been asking for. I have no idea if that is compatible with json encoding.
Jon Surrell 9,0247 gold badges47 silver badges53 bronze badges answered Apr 10, 2011 at 11:54
hakrehakre 187k48 gold badges419 silver badges803 bronze badges If you are generating JSON, why not just use the
answered Apr 10, 2011 at 11:36
Pascal MARTINPascal MARTIN 388k77 gold badges646 silver badges656 bronze badges 4
To use the function you simply need to use
Jay Gilford 15.1k6 gold badges35 silver badges55 bronze badges answered Oct 5, 2012 at 14:19
1 How do you escape quotes in PHP?In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.
Should I use single or double quotes PHP?In PHP, people use single quote to define a constant string, like 'a' , 'my name' , 'abc xyz' , while using double quote to define a string contain identifier like "a $b $c $d" . echo "my $a"; This is true for other used of string.
What is the use of Addslashes in PHP?The addslashes() function returns a string with backslashes in front of predefined characters. The predefined characters are: single quote (')
How do I backslash a string in PHP?The simplest way to specify a string is to enclose it in single quotes (the character ' ). To specify a literal single quote, escape it with a backslash ( \ ). To specify a literal backslash, double it ( \\ ).
|