php 如何将 HTML 标签显示为纯文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6817262/
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
How to display HTML tags as plain text
提问by user517593
I have an input form on my website where HTML is allowed and I'm trying to add instructions about the use of HTML tags. I'd like the text to
我的网站上有一个允许使用 HTML 的输入表单,我正在尝试添加有关使用 HTML 标签的说明。我想要文字
<strong>Look just like this line - so then know how to type it</strong>
But so far all I get is:
但到目前为止,我得到的是:
Look just like this line - so then know how to type it
看起来就像这一行 - 所以知道如何输入
How can I show the tags so people know what to type?
如何显示标签以便人们知道要键入的内容?
回答by Darm
Replace <
with <
and >
with >
.
更换<
用<
和>
用>
。
回答by acme
In PHP use the function htmlspecialchars() to escape <
and >
.
在 PHP 中使用函数htmlspecialchars() 来转义<
和>
.
htmlspecialchars('<strong>something</strong>')
回答by Jarrod
As many others have said, htmlentities()
will do the trick... but it will look like shit.
正如许多其他人所说,htmlentities()
会成功……但它看起来很糟糕。
Wrap it up with a <pre>
tag and you'll preserve your indentation.
用一个<pre>
标签把它包起来,你会保留你的缩进。
echo '<pre>';
echo htmlspecialchars($YOUR_HTML);
echo '</pre>';
回答by Luiz Damim
You should use htmlspecialchars
. It replaces characters as below:
你应该使用htmlspecialchars
. 它替换字符如下:
- '&' (ampersand) becomes
&
- '"' (double quote) becomes
"
when ENT_NOQUOTES is not set. - "'" (single quote) becomes
'
only when ENT_QUOTES is set. - '<' (less than) becomes
<
- '>' (greater than) becomes
>
- '&'(与号)变成
&
- '"'(双引号)
"
在未设置 ENT_NOQUOTES 时变为。 - “'”(单引号)
'
仅在设置了 ENT_QUOTES 时才变为。 - '<'(小于)变成
<
- '>'(大于)变成
>
回答by Nikunj K.
you may use htmlspecialchars()
你可以使用 htmlspecialchars()
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
回答by James Montagne
You just need to encode the <>
s:
您只需要对<>
s进行编码:
<strong>Look just like this line - so then know how to type it</strong>
回答by Paulo Henrique
To display HTML tags within a browser, surround the output with < xmp> and < / xmp> tags
要在浏览器中显示 HTML 标签,请使用 <xmp> 和 </xmp> 标签将输出括起来
回答by martynthewolf
You can use htmlentities when echoing to the browser, this will show the tag rather than have html interpret it.
您可以在回显到浏览器时使用 htmlentities,这将显示标签而不是让 html 解释它。
See here http://uk3.php.net/manual/en/function.htmlentities.php
见这里http://uk3.php.net/manual/en/function.htmlentities.php
Example:
例子:
echo htmlentities("<strong>Look just like this line - so then know how to type it</strong>");
Output:
输出:
<strong>Look just like this line - so then know how to type it</strong>
回答by Reuben
Use htmlentities()to convert characters that would otherwise be displayed as HTML.
使用htmlentities()转换原本会显示为 HTML 的字符。
回答by Alessandro
There is another way...
还有一种方法...
header('Content-Type: text/plain; charset=utf-8');
This makes the whole page served as plain text... better is htmlspecialchars...
这使得整个页面作为纯文本......更好的是htmlspecialchars......
Hope this helps...
希望这可以帮助...