我的网站上有一个允许HTML的输入表单,我正在尝试添加有关HTML标记使用的说明 . 我想要的文字
<strong>Look just like this line - so then know how to type it</strong>
但到目前为止,我得到的是:
Look just like this line - so then know how to type it
如何显示标签,以便人们知道要键入什么?
正如许多人所说的那样, htmlentities() 会做到这一点......但它看起来很糟糕 .
htmlentities()
用 <pre> 标签包裹它,你会保留你的缩进 .
<pre>
echo '<pre>'; echo htmlspecialchars($YOUR_HTML); echo '</pre>';
要在浏览器中显示HTML标记,请使用<xmp>和</ xmp>标记包围输出
在PHP中使用函数htmlspecialchars()来转义 < 和 > .
<
>
htmlspecialchars('<strong>something</strong>')
你只需要编码 <> :
<>
<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>");
输出:
使用htmlentities()转换否则将显示为HTML的字符 .
你应该使用htmlspecialchars . 它替换如下字符:
'&'(&符号)变为 &当未设置ENT_NOQUOTES时,
&
'"'(双引号)变为 " .仅当设置了ENT_QUOTES时,
"
"'"(单引号)才变为 ' .
'
'<'(小于)变为 <
'>'(大于)变为 >
你可以使用htmlspecialchars()
<?php $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES); echo $new; // <a href='test'>Test</a> ?>
还有另一种方式......
header('Content-Type: text/plain; charset=utf-8');
这使整个页面作为纯文本...更好的是htmlspecialchars ...
希望这可以帮助...
10 回答
正如许多人所说的那样,
htmlentities()
会做到这一点......但它看起来很糟糕 .用
<pre>
标签包裹它,你会保留你的缩进 .要在浏览器中显示HTML标记,请使用<xmp>和</ xmp>标记包围输出
在PHP中使用函数htmlspecialchars()来转义
<
和>
.你只需要编码
<>
:由
<
和>
替换<
由>
您可以在回显到浏览器时使用htmlentities,这将显示标记而不是让html解释它 .
看这里http://uk3.php.net/manual/en/function.htmlentities.php
例:
输出:
使用htmlentities()转换否则将显示为HTML的字符 .
你应该使用htmlspecialchars . 它替换如下字符:
'&'(&符号)变为
&
当未设置ENT_NOQUOTES时,
'"'(双引号)变为
"
.仅当设置了ENT_QUOTES时,
"'"(单引号)才变为
'
.'<'(小于)变为
<
'>'(大于)变为
>
你可以使用htmlspecialchars()
还有另一种方式......
这使整个页面作为纯文本...更好的是htmlspecialchars ...
希望这可以帮助...