0

我希望能够在我的 HTML 页面中编写 javascript、php 和其他源代码,但不能让它运行。aka,它只是在页面上完全可读,而不是被转换成代码并显示出来。

例如,我想<php echo "Hello World!"; ?> 出现,而不是作为一个 hello world 程序运行。

4

3 回答 3

2

尝试:

echo htmlentities('<?php echo "Hello World!"; ?>');
于 2013-04-23T23:48:55.190 回答
1
<pre><code>code here</code></pre>
于 2013-04-23T23:52:43.800 回答
1

执行以下文本替换,或至少执行前两个。(PHPhtmlspecialchars函数执行前四个以允许使用双引号时的转义文本作为属性值。)请参阅MDN Introduction to HTML的“命名字符引用”部分。

  • &&amp;
  • <&lt;
  • >&gt;
  • "&quot;
  • '使用&#39;&#039;(或&apos;在不必验证为 HTML 4 的文档中)

然后将代码包围在<pre>...</pre>.


一些示例代码:

<script>
var url = location.href;
if (url.indexOf('hello') >= 0 && url.indexOf('world') >= 0) {
    alert('hello, world');
}
</script>

变成

<pre>
&lt;script type="text/javascript">
var url = location.href;
if (url.indexOf('hello') >= 0 &amp;&amp; url.indexOf('world') >= 0) {
    alert('hello, world');
}
&lt;/script>
</pre>

或者

<pre>
&lt;script type=&quot;text/javascript&quot;&gt;
var url = location.href;
if (url.indexOf(&#39;hello&#39;) &gt;= 0 &amp;&amp; url.indexOf(&#39;world&#39;) &gt;= 0) {
    alert(&#39;hello, world&#39;);
}
&lt;/script&gt;
</pre>
于 2013-04-24T00:12:11.240 回答