如何在使用单引号的回声中使用单引号

问题描述 投票:13回答:5

首先,我已经完成了相关的问题..没有找到任何答案..我使用此代码来显示消息

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

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

php echo quotes
5个回答
26
投票

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

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

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

5
投票

使用反斜杠转义引号。

'hello\'s'

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


2
投票

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

我个人更喜欢功能htmlspecialchars(),它做同样的事情,但有标志,让你指定它的行为。

像这样:

echo htmlspecialchars(“O'Rielly”,ENT_QUOTES);

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


1
投票
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


1
投票

在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 ";

0
投票

由于答案中的方法对我的情况不起作用,所以每次通过代码更改引用类型并交换引用类型以启动回声时,我最终只调用一个新的回声,它现在是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>';
        }
© www.soinside.com 2019 - 2024. All rights reserved.