首页 文章

如何将HTML标记显示为纯文本

提问于
浏览
161

我的网站上有一个允许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

如何显示标签,以便人们知道要键入什么?

10 回答

  • 6

    正如许多人所说的那样, htmlentities() 会做到这一点......但它看起来很糟糕 .

    <pre> 标签包裹它,你会保留你的缩进 .

    echo '<pre>';
    echo htmlspecialchars($YOUR_HTML);
    echo '</pre>';
    
  • 33

    要在浏览器中显示HTML标记,请使用<xmp>和</ xmp>标记包围输出

  • 0

    在PHP中使用函数htmlspecialchars()来转义 <> .

    htmlspecialchars('<strong>something</strong>')
    
  • 226

    你只需要编码 <>

    &lt;strong&gt;Look just like this line - so then know how to type it&lt;/strong&gt;
    
  • 209

    &lt;> 替换 <&gt;

  • 1

    您可以在回显到浏览器时使用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>
    
  • 3

    使用htmlentities()转换否则将显示为HTML的字符 .

  • 45

    你应该使用htmlspecialchars . 它替换如下字符:

    • '&'(&符号)变为 &amp;
      当未设置ENT_NOQUOTES时,

    • '"'(双引号)变为 &quot; .
      仅当设置了ENT_QUOTES时,

    • "'"(单引号)才变为 &#039; .

    • '<'(小于)变为 &lt;

    • '>'(大于)变为 &gt;

  • 4

    你可以使用htmlspecialchars()

    <?php
    $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
    echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
    ?>
    
  • 13

    还有另一种方式......

    header('Content-Type: text/plain; charset=utf-8');
    

    这使整个页面作为纯文本...更好的是htmlspecialchars ...

    希望这可以帮助...

相关问题