将 HTML 显示为代码
我构建了一个简单的网站,允许用户共享代码,其中代码以 HTML 形式保存在数据库中。例如,
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=1024">
<title></title>
</head>
<body id="" class="">
</body>
</html>
我遇到的问题是当我将数据库吐出的内容包装在
中时,将其显示在网站上供其他人查看它仍然呈现 HTML 并且不将其视为要显示的代码。我读过您必须使用 >
来解决这个问题,但是这是不可行的,因为用户将共享原始代码并且无法开始将代码转换为 html像这样的实体!我该如何解决这个问题?谢谢。
以下是将其插入数据库的表单代码: http://pastebin.com/i3pn2AjT
和这个这是我目前的显示方式: <代码>
I have built a simple site that allows users to share code where the code is saved in the database as HTML. e.g.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=1024">
<title></title>
</head>
<body id="" class="">
</body>
</html>
The problem I have is showing it on the site for others to see as when I wrap what the database spits out inside <code><pre></pre></code>
it still renders the HTML and doesn't treat it as code to display. I have read that you have to use >
to get around this, but this isn't feasible as the users will be sharing raw code and will not be able to start converting the code to html entities like that! how do i fix this? Thanks.
Here is the code for the form to inserts this into the DB: http://pastebin.com/i3pn2AjT
and this is how I display it currently:<div class="code">
<pre>
<?php echo get_post_meta($posts_post_id, 'post_code', true); ?>
</pre>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在显示时或插入时转义 HTML 代码。
从您之前的问题来看,您似乎正在使用 PHP。您可以使用
htmlentities()
来实现此目的。例如:
You need to escape the HTML code either at display time or at insert time.
From your previous questions it seems you are using PHP. You can achieve this with
htmlentities()
.For example:
您可以尝试使用 php 函数“
highlight_string();
”,如下所示:它会将 ('input') 输出为带有颜色编码的字符串。
查看其 php 文档: http://php.net/manual/en/ function.highlight-string.php
You can try the php function "
highlight_string();
" like this:It will output the ('input') as a string with colorcoding.
check out its php documentation: http://php.net/manual/en/function.highlight-string.php