将HTML显示为代码

Cam*_*ron 1 html pre

我已经构建了一个简单的站点,允许用户共享代码,将代码保存在数据库中作为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>
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是在网站上显示它以供其他人看到,当我将数据库吐出的内容包装在其中时,<code><pre></pre></code>它仍会呈现HTML并且不会将其视为要显示的代码.我已经读过你必须使用它&gt;来解决这个问题,但这是不可行的,因为用户将共享原始代码,并且无法开始将代码转换为像这样的html实体!我该如何解决?谢谢.

以下是将其插入数据库的表单代码:http://pastebin.com/i3pn2AjT

这就是我目前的显示方式: <div class="code"> <pre> <?php echo get_post_meta($posts_post_id, 'post_code', true); ?> </pre> </div>

And*_*ore 7

您需要在显示时或插入时转义HTML代码.

从您之前的问题看来,您似乎正在使用PHP.你可以实现这一目标htmlentities().

例如:

<code><?php echo htmlentities($userSuppliedHTML, ENT_QUOTES, 'UTF-8'); ?></code>
Run Code Online (Sandbox Code Playgroud)