【发布时间】:2011-10-12 15:14:04
【问题描述】:
我的网站上有一个允许使用 HTML 的输入表单,我正在尝试添加有关使用 HTML 标记的说明。我想要文本到
<strong>Look just like this line - so then know how to type it</strong>
但到目前为止,我得到的只是:
看起来就像这一行 - 然后知道如何输入它
如何显示标签以便人们知道要输入什么内容?
【问题讨论】:
我的网站上有一个允许使用 HTML 的输入表单,我正在尝试添加有关使用 HTML 标记的说明。我想要文本到
<strong>Look just like this line - so then know how to type it</strong>
但到目前为止,我得到的只是:
看起来就像这一行 - 然后知道如何输入它
如何显示标签以便人们知道要输入什么内容?
【问题讨论】:
您应该使用htmlspecialchars。它替换字符如下:
&amp;(和号)变为&amp;
"(双引号)变为 &quot;。'(单引号)才会变为 &#039;。&lt;(小于)变为&lt;
&gt;(大于)变为&gt;
【讨论】:
原生 JavaScript 方法 -
('<strong>Look just ...</strong>').replace(/</g, '<').replace(/>/g, '>');
享受吧!
【讨论】:
将&lt; 替换为&lt;,将&gt; 替换为&gt;。
【讨论】:
正如许多其他人所说,htmlentities() 可以解决问题……但看起来很糟糕。
用<pre> 标记将其包裹起来,您将保留缩进。
echo '<pre>';
echo htmlspecialchars($YOUR_HTML);
echo '</pre>';
【讨论】:
还有一种方法……
header('Content-Type: text/plain; charset=utf-8');
这使得整个页面成为纯文本...更好的是 htmlspecialchars...
希望这会有所帮助...
【讨论】:
要在浏览器中显示 HTML 标记,请用
【讨论】:
<xmp> 现在已经过时了,不仅仅是被弃用了。不要使用它。
你可以使用 htmlspecialchars()
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
【讨论】:
您可以在回显到浏览器时使用 htmlentities,这将显示标签而不是让 html 解释它。
请看这里http://uk3.php.net/manual/en/function.htmlentities.php
例子:
echo htmlentities("<strong>Look just like this line - so then know how to type it</strong>");
输出:
<strong>Look just like this line - so then know how to type it</strong>
【讨论】:
使用htmlentities() 转换原本会显示为 HTML 的字符。
【讨论】:
在 PHP 中使用函数 htmlspecialchars() 转义 < 和 >。
htmlspecialchars('<strong>something</strong>')
【讨论】:
您只需对<>s 进行编码:
<strong>Look just like this line - so then know how to type it</strong>
【讨论】: