【问题标题】:How to add slashes for quotes in a string using PHP? [duplicate]如何使用 PHP 为字符串中的引号添加斜杠? [复制]
【发布时间】:2011-09-14 09:06:55
【问题描述】:

可能重复:
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\"';

【问题讨论】:

  • 尝试在谷歌输入你的问题标题
  • 基本问题...下次试试谷歌。现在,看看PHP的addslashes方法
  • addslashes 不正确,会替换更多。因此,所有因此而渴望关闭的人(迄今为止唯一建议的重复项)可能搜索得太快了 :)

标签: php html escaping backslash


【解决方案1】:
$one = str_replace('"', '\"', $one);

【讨论】:

    【解决方案2】:

    要添加斜线,请使用

    $one = addslashes($one);
    

    或者删除

    $one = stripslashes($one);
    

    【讨论】:

      【解决方案3】:

      您要查找的函数是str_replaceaddcslashes

      $one = 'put "returns" between "paragraphs"';
      
      $slashed = addcslashes($one, '"');
      
      echo $slashed;
      

      Demo.

      【讨论】:

        猜你喜欢
        • 2016-08-19
        • 1970-01-01
        • 2013-06-18
        • 2014-07-26
        • 1970-01-01
        • 2011-01-12
        • 1970-01-01
        • 1970-01-01
        • 2010-10-13
        相关资源
        最近更新 更多