简体   繁体   English

显示HTML代码

[英]Displaying HTML code

如何阻止HTML代码呈现并将其显示为标准文本,以便我的用户只需复制并粘贴它们以供自己使用?

You could use the function htmlentities() . 您可以使用函数htmlentities() Have a look at the manual . 看一下手册

Please note that by default the data will be returned using the ISO-8859-1 character set. 请注意,默认情况下,将使用ISO-8859-1字符集返回数据。 You might want to change the third parameter to UTF-8 . 您可能希望将第三个参数更改为UTF-8

For example: 例如:

$html = "<div>Some text</div>";

echo htmlentities($html);

使用PHP设置内容类型:

header("Content-Type: text/plain");

You parse it into markup which uses HTML entities, for example instead of using: 您将其解析为使用HTML实体的标记,例如,而不是使用:

<body>

you would instead use entity codes to escape the markup characters, like this: 你会改为使用实体代码来转义标记字符,如下所示:

&lt;body&gt;

There are plenty of references to be found which list entity codes, and it's pretty easy to parse and escape HTML in most programming languages. 列表实体代码有很多参考资料,在大多数编程语言中解析和转义HTML非常容易。

HTML code can be displayed using php script by using functions htmlentities() and htmlspecialchars() . 可以使用函数htmlentities()htmlspecialchars()使用php脚本显示HTML代码。 For understanding this concept consider the following example: 要了解此概念,请考虑以下示例:

<?php
$a="<h1>heading tag: used for heading tags</h1>";
echo htmlentities($a)."<br/>";
echo htmlspecialchars($a);
?>

Output will be: 输出将是:

<h1>heading tag: used for heading tags</h1>
<h1>heading tag: used for heading tags</h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM