开发者

How to add slashes for quotes in a string using PHP? [duplicate]

This question already has answers here: Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜