我的网站上有一个输入表单,允许使用HTML,并且我正在尝试添加关于如何使用HTML标签的说明。我希望文本
<strong>Look just like this line - so then know how to type it</strong>
但是到目前为止,我得到的都是:
看起来就像这一行 - 所以知道如何输入它
我如何显示标签,让人们知道要输入什么?
将<
替换为<
,将>
替换为>
。
正如许多人所说,htmlentities()
可以解决问题... 但它会看起来很糟糕。
用<pre>
标签把代码包起来,就可以保留缩进了。
echo '<pre>';
echo htmlspecialchars($YOUR_HTML);
echo '</pre>';
htmlspecialchars
函数。它将字符替换为以下内容:
&
(和号)将变成 &
"
(双引号)如果 ENT_NOQUOTES 没有设置,则变成 "
'
(单引号)如果 ENT_QUOTES 设置了,则变成 '
<
(小于号)将变成 <
>
(大于号)将变成 >
您可以使用htmlspecialchars()函数
<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
?>
您只需要对<>
进行编码:
<strong>Look just like this line - so then know how to type it</strong>
当向浏览器输出内容时,您可以使用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>
('<strong>Look just ...</strong>').replace(/</g, '<').replace(/>/g, '>');
请享用!