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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11792990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:07:33  来源:igfitidea点击:

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

phpechoquotes

提问by Nasir Zia

First of all, i have gone through the related questions.. haven't found any answer.. I m using this code to display a message

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

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

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

How can i make this work, as any quote inside this echo will break the statement...

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

回答by Madara's Ghost

Either escape the quote with a backslash, or use double quotes to designate the string.

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

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

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

回答by He Hui

Escape the quote using a backslash.

使用反斜杠转义引号。

'hello\'s'

The single quote that appears after the backslash will appear on screen.

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

回答by john ktejik

Have you tried the function addslashes()? It even uses your example.

您是否尝试过函数addlashes()?它甚至使用你的例子。

I personally prefer the function htmlspecialchars()which does the same thing but has flags which let you specify its behavior.

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

like so:

像这样:

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

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

This shows the string properly on an HTML webpage.

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

回答by Jalpesh Patel

In PHP, the escape sequence character is the backslash (\). You can add this before special characters to ensure that those characters are displayed as literals. For example:

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

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

Or you can also write like this:

或者你也可以这样写:

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

回答by Prasanth

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;

Be sure to read thisbefore using such kind of strings.

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

回答by Pedro Cantante

Since the methods from the answers didn't work for my situation I ended up just calling a new echo everytime the type of quote changed through the code and swapped the type of quote to start the echo, it's 2019 now and I don't know about any other solution since I am really new to programming but this worked fine for me, example:

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

回答by user13550601

The problem may be that you are passing the variable through HTML in single quotes ''. If it is the case, try using double quotes: "".

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