如何在网页上显示HTML片段,而不需要将每个<替换为&lt;>和&gt;?

换句话说,是否有一个标记表示在点击结束标记之前不呈现HTML ?


当前回答

我是这样做的:

$str = file_get_contents("my-code-file.php");
echo "<textarea disabled='true' style='border: none;background-color:white;'>";
echo $str;
echo "</textarea>";

其他回答

我认为:

你想要编写100%有效的HTML5 你想把代码片段(几乎)文字放在HTML中 特别是<应该不需要转义

你所有的选项都在这棵树里:

with HTML syntax there are five kinds of elements those called "normal elements" (like <p>) can't have a literal < it would be considered the start of the next tag or comment void elements they have no content you could put your HTML in a data attribute (but this is true for all elements) that would need JavaScript to move the data elsewhere in double-quoted attributes, " and &thing; need escaping: &quot; and &amp;thing; respectively raw text elements <script> and <style> only they are never rendered visible but embedding your text in Javascript might be feasable Javascript allows for multi-line strings with backticks it could then be inserted dynamically a literal </script is not allowed anywhere in <script> escapable raw text elements <textarea> and <title> only <textarea> is a good candidate to wrap code in it is totally legal to write </html> in there not legal is the substring </textarea for obvious reasons escape this special case with &lt;/textarea or similar &thing; needs escaping: &amp;thing; foreign elements elements from MathML and SVG namespaces at least SVG allows embedding of HTML again... and CDATA is allowed there, so it seems to have potential with XML syntax covered by Konrad's answer

 

注意:>从来不需要转义。甚至在正常元素中也没有。

这是一种hack,但是我们可以使用类似的东西:

正文脚本{ 显示:块; 字体类型:等宽字体; 空白:前; } <脚本type = " text / html " > <标题> Hello World h1 > < / < ul > <li>享受这个狡猾的黑客, <李>或不! < / ul > > < /脚本

使用该CSS,浏览器将在主体中显示脚本。它不会尝试执行这个脚本,因为它有一个未知的类型text/html。在<script>中没有必要转义特殊字符,除非你想包含一个结束</script>标记。

我正在使用类似这样的东西在页面主体中显示可执行的JavaScript,这是一种“有文化的编程”。

在这个问题中还有更多的信息,标签什么时候应该可见,为什么可以?

最终,最好的答案(尽管很烦人)是“转义文本”。

然而,有很多文本编辑器——甚至是独立的迷你实用程序——可以自动完成这项工作。所以如果你不想,你永远都不应该手动转义它(除非它是转义和未转义代码的混合…)

快速搜索谷歌会显示这个,例如:http://malektips.com/zzee-text-utility-html-escape-regular-expression.html

它可能并不适用于所有情况,但将代码片段放在文本区域内将它们显示为代码。

如果你不想让它看起来像一个实际的文本区域,你可以用CSS样式文本区域。

实际上有一种方法可以做到。它有一个限制(1),但是是100%标准的,没有被弃用(像xmp一样),并且可以工作。

这是微不足道的。下面就是:

<div id="mydoc-src" style="display: none;">
LlNnlljn77fggggkk77csJJK8bbJBKJBkjjjjbbbJJLJLLJo
<!--
YOUR CODE HERE.
    <script src="WidgetsLib/all.js"></script>
    ^^ This is a text, no side effects trying to load it.
-->
LlNnlljn77fggggkk77csJJK8bbJBKJBkjjjjbbbJJLJLLJo
</div>

请听我解释。首先,普通的HTML注释可以防止整个块被解释。你可以很容易地在其中添加任何标签,它们都将被忽略。从解释中忽略,但仍然可以通过innerHTML!因此,剩下的就是获取内容,并过滤前面和后面的注释标记。

除了(记住-限制)你不能把里面的HTML注释,因为(至少在我的Chrome)他们的嵌套是不支持的,和非常第一个'——>'将结束显示。

好吧,这是一个讨厌的小限制,但在某些情况下,如果你的文本没有HTML注释,这根本不是问题。而且,逃避一个构念比逃避一堆构念容易。

Now, what is that weird LlNnlljn77fggggkk77csJJK8bbJBKJBkjjjjbbbJJLJLLJo string? It's a random string, like a hash, unlikely to be used in the block, and used for? Here's the context, why I have used it. In my case, I took the contents of one DIV, then processed it with Showdown markdown, and then the output assigned into another div. The idea was, to write markdown inline in the HTML file, and just open in a browser and it would transform on the load on-the-fly. So, in my case, <!-- became transformed to <p><!--</p>, the comment properly escaped. It worked, but polluted the screen. So, to easily remove it with regex, the random string was used. Here's the code:

    var converter = new showdown.Converter();
    converter.setOption('simplifiedAutoLink', true);
    converter.setOption('tables', true);
    converter.setOption('tasklists', true);
    var src = document.getElementById("mydoc-src");
    var res = document.getElementById("mydoc-res");
    res.innerHTML = converter.makeHtml(src.innerHTML)
            .replace(/<p>.{0,10}LlNnlljn77fggggkk77csJJK8bbJBKJBkjjjjbbbJJLJLLJo.{0,10}<\/p>/g, "");
    src.innerHTML = '';

这很有效。

如果有人感兴趣,本文就是使用这种技术编写的。请随意下载,并查看HTML文件。

这取决于你用它来做什么。是用户输入吗?然后使用<textarea>,并转义所有内容。在我的例子中(可能也是您的例子),我只是使用了注释,它就完成了工作。

如果你不使用markdown,只是想从标签中得到它,那么它甚至更简单:

<div id="mydoc-src" style="display: none;">
<!--
YOUR CODE HERE.
    <script src="WidgetsLib/all.js"></script>
    ^^ This is a text, no side effects trying to load it.
-->
</div>

和JavaScript代码来获取它:

    var src = document.getElementById("mydoc-src");
    var YOUR_CODE = src.innerHTML.replace(/(<!--|-->)/g, "");