How to add slashes for quotes in a string using PHP? [duplicate]
Possible Duplicate:
quick function to replace ' with \&开发者_运维问答#39; in php Is there a PHP function that only adds slashes to double quotes NOT single quotes
I have for example:
$one = 'put "returns" between "paragraphs"';
$two = '"linebreak" add 2 spaces "at end"';
How can I convert this for:
$one = 'put \"returns\" between \"paragraphs\"';
$two = '\"linebreak\" add 2 spaces \"at end\"';
$one = str_replace('"', '\"', $one);
To add the slashes, use
$one = addslashes($one);
Or to remove
$one = stripslashes($one);
The function you're looking for is either str_replace
or addcslashes
:
$one = 'put "returns" between "paragraphs"';
$slashed = addcslashes($one, '"');
echo $slashed;
Demo.
精彩评论