【问题标题】:Replace single quote with double quote php用双引号php替换单引号
【发布时间】:2018-05-12 23:31:23
【问题描述】:

这是我的html代码:

 <div style="box-sizing: border-box; 
                  color: rgb(37, 37, 37); 
            font-family: Axiforma-Regular; 
              font-size: 16px;">
 </div>

我想用单引号替换这些内联 css 双引号。

我试过了:

$display_box_content = '<div style="box-sizing: border-box; 
                                         color: rgb(37, 37, 37); 
                                   font-family: Axiforma-Regular; 
                                     font-size: 16px;">
                        </div>'

$display_box_content = str_replace('"', "'", $display_box_content);

但这些不起作用。

请帮忙!

【问题讨论】:

标签: php


【解决方案1】:

您在第 1 行缺少;

$display_box_content = '<div style="box-sizing: border-box; color: rgb(37, 37, 37); font-family: Axiforma-Regular; font-size: 16px;"></div>'; //added a ; here
$display_box_content = str_replace('"', "'", $display_box_content);   

这段代码确实有效;请看3v4l

【讨论】:

    【解决方案2】:

    如果你想在 php 字符串中用双引号替换单引号,你可以这样做。

      $replacedString = str_replace(chr(39), chr(34),$display_box_content);
      //chr(39) implies single quote 
      //chr(34) implies double quote
    

    您可以从this link 阅读 php 中的 chr() 函数

    【讨论】:

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

      【讨论】:

      【解决方案4】:

      我正在使用这样的htmlentites函数:

      $display_box_content = htmlentities($display_box_content);
      $display_box_content = str_replace('"', "'", $display_box_content);
      

      所以我只是将这些行换成了这个:

      $display_box_content = str_replace('"', "'", $display_box_content);
      $display_box_content = htmlentities($display_box_content);
      

      它奏效了。谢谢大家的帮助!

      【讨论】:

        【解决方案5】:

        你只需要使用单引号吗?

        $display_box_content = "<div style='box-sizing: border-box; color: rgb(37, 37, 37); font-family: Axiforma-Regular; font-size: 16px;'></div>"
        

        【讨论】:

        • 正如@Gyandeep Sharma 已经提到的我刚刚看到的!他更快;)
        • 问题是关于使用 str_replace 更改它
        猜你喜欢
        • 2021-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 2018-09-23
        • 2011-01-26
        • 2013-04-15
        相关资源
        最近更新 更多