【问题标题】:How to use single quote inside an echo which is using single quote如何在使用单引号的回声中使用单引号
【发布时间】:2012-08-01 08:10:57
【问题描述】:

首先,我已经解决了相关问题..没有找到任何答案.. 我正在使用此代码显示消息

echo 'Here goes your message with an apostrophe S like thi's ';

我怎样才能做到这一点,因为这个回声中的任何引用都会破坏声明......

【问题讨论】:

  • echo 'Here goes your message with an apostrophe S like thi\'s ';
  • +1 K102。您可以阅读 that 并且,您想在 echo 函数中使用 doubleQuote : echo " it's me!" ;

标签: php echo quotes


【解决方案1】:

要么用反斜杠转义引号,要么使用双引号来指定字符串。

echo 'Here goes your message with an apostrophe S like thi\'s';

echo "Here goes your message with an apostrophe S like thi's";

【讨论】:

    【解决方案2】:

    使用反斜杠转义引号。

    'hello\'s'
    

    反斜杠后出现的单引号将出现在屏幕上。

    【讨论】:

      【解决方案3】:

      在 PHP 中,转义序列字符是反斜杠 (\)。您可以在特殊字符之前添加它,以确保这些字符显示为文字。例如:

      echo 'Here goes your message with an apostrophe S like thi\'s ';
      

      或者你也可以这样写:

      echo "Here goes your message with an apostrophe S like thi's ";
      

      【讨论】:

        【解决方案4】:
        echo <<<EOT
        You can put what ever you want here.. HTML, " ' ` anyting will go
        Here goes your message with an apostrophe S like thi's
        EOT;
        

        在使用此类字符串之前,请务必阅读this

        【讨论】:

          【解决方案5】:

          你试过addslashes()这个功能吗?它甚至使用你的例子。

          我个人更喜欢htmlspecialchars()这个函数 它做同样的事情,但有允许你指定其行为的标志。

          像这样:

          echo htmlspecialchars("O'Rielly", ENT_QUOTES);

          这会在 HTML 网页上正确显示字符串。

          【讨论】:

            【解决方案6】:

            由于答案中的方法不适用于我的情况,我最终只是在每次通过代码更改报价类型并交换报价类型以启动回声时调用一个新的回声,现在是 2019 年,我没有'不知道任何其他解决方案,因为我对编程真的很陌生,但这对我来说很好,例如:

                    else {
                      echo '<a onclick="document.getElementById(';
                      echo "'open_login').style.display='block'";
                      echo '" class="branding w3-bar-item w3-button w3-mobile w3-light-blue w3-hover-white w3-right"href="#login"><span class="fa fa-user"></span>&nbsp;&nbsp;Login do Aluno</a>';
                    }

            【讨论】:

              【解决方案7】:

              问题可能是您在 HTML 中用单引号 '' 传递变量。如果是这种情况,请尝试使用双引号:“”。

              【讨论】:

              • 欢迎来到 StackOverflow。请查看问题的现有答案。尽量不要重复这些,尤其是在 8 年前的问题上。
              猜你喜欢
              • 1970-01-01
              • 2011-03-23
              • 2012-03-26
              • 2017-12-07
              相关资源
              最近更新 更多