开发者

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

开发者 https://www.devze.com 2023-04-04 14:19 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: quick function to replace ' with \\&开发者_运维问答#39; in php
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

精彩评论

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