如何在使用单引号的echo中使用单引号


How to use single quote inside an echo which is using single quote

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

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

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

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

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

使用反斜杠转义引号。

'hello''s'

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

您尝试过函数addslashes()吗?它甚至用了你的例子。

我个人更喜欢函数htmlspecialchar()它做同样的事情,但有一些标志可以让你指定它的行为。

像这样:

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

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

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;

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

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

由于答案中的方法不适用于我的情况,每次代码中的引号类型发生变化并交换引号类型以启动echo时,我都会调用一个新的echo,现在是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>';
        }

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