如何使用PHP在字符串中为引号添加斜杠?

时间:2011-09-14 09:06:56

标签: php html escaping backslash

  

可能重复:
  quick function to replace ' with \' in php
  Is there a PHP function that only adds slashes to double quotes NOT single quotes

我有例如:

$one = 'put "returns" between "paragraphs"';

$two = '"linebreak" add 2 spaces "at end"';

如何将其转换为:

$one = 'put \"returns\" between \"paragraphs\"';

$two = '\"linebreak\" add 2 spaces \"at end\"';

3 个答案:

答案 0 :(得分:5)

要添加斜杠,请使用

$one = addslashes($one);

或删除

$one = stripslashes($one);

答案 1 :(得分:5)

$one = str_replace('"', '\"', $one);

答案 2 :(得分:4)

您正在寻找的功能是str_replaceaddcslashes

$one = 'put "returns" between "paragraphs"';

$slashed = addcslashes($one, '"');

echo $slashed;

Demo