如何在页面上显示 PHP 和 HTML 源代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15621630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display PHP & HTML source code on a page?
提问by Qpert
回答by Musa
You can use html entities <?php
in the html it will be rendered as <?php
您可以<?php
在 html 中使用 html 实体,它将呈现为 <?php
You can use htmlspecialcharsto encode your code to use html entities.
您可以使用htmlspecialchars对代码进行编码以使用 html 实体。
回答by jszobody
Use <pre>
or <code>
tags to wrap your code.
使用<pre>
或<code>
标签来包装您的代码。
Take a look at http://php.net/manual/en/function.highlight-string.phpto further see how you can make the code look pretty.
查看http://php.net/manual/en/function.highlight-string.php以进一步了解如何使代码看起来更漂亮。
Since passing a large block of code to highlight_string() can be messy, you may want to look at output buffering in combination with highlight_string to output colorized php code.
由于将一大块代码传递给 highlight_string() 可能会很麻烦,因此您可能希望将输出缓冲与 highlight_string 结合使用以输出彩色的 php 代码。
Something like:
就像是:
<?php
ob_start();
?>
phpinfo();
echo "this echo statement isn't executed";
<?php
$code = ob_get_clean();
highlight_string($code);
?>
回答by Salman Mohammad
Simply you can use following code to display php code on webpage.
只需使用以下代码即可在网页上显示 php 代码。
highlight_string("<?php print('This is php code.'); ?>");
It will give output like
它会给出类似的输出
<?php print('This is php code.'); ?>
回答by David
The first step is to not wrap that code in PHP tags. So instead of this:
第一步是不要将该代码包装在 PHP 标签中。所以而不是这个:
<?
var sample = "code";
?>
You would have this:
你会有这个:
var sample = "code";
It's not the code itself which triggers the server-side compile from the PHP engine, it's the tags which indicate to that engine what blocks of the file are code and what are not. Anything that's not code is essentially treated as a string and output to the page as-is for the browser to interpret.
不是代码本身触发了 PHP 引擎的服务器端编译,而是标记向该引擎指示文件的哪些块是代码,哪些不是。任何不是代码的内容本质上都被视为字符串并按原样输出到页面以供浏览器解释。
Once you're outputting the code, it's then a matter of formatting it. The old standard is to wrap it in pre
tags to get rid of HTML-ish formatting:
输出代码后,接下来就是对其进行格式化的问题。旧标准是将其包装在pre
标签中以摆脱 HTML-ish 格式:
<pre>
var sample = "code";
</pre>
You can also apply CSS style to the pre
tags (or any other tags you want to use for displaying code, such as div
) as you see fit.
您还可以将 CSS 样式应用到您认为合适的pre
标签(或您想要用于显示代码的任何其他标签,例如div
)。
There are also very useful code syntax highlighting plugins and tools to make the code a lot "prettier". Google-code-prettifyoften comes highly recommended.
还有非常有用的代码语法突出显示插件和工具,可以使代码“更漂亮”。 强烈推荐Google-code-prettify。
回答by Saad El Yousfi
use the header function of php, this will rea
使用php的header函数,这样就可以了
<?php
header("content-type: text/plain");
?>
回答by Ashfak Md Shibli
You can use this template........
你可以使用这个模板......
######################################################################
echo "<h2><br>Source Code of ".basename((string)__FILE__) . "</h2><hr>";
show_source(__FILE__);
echo "<hr>";
echo "<h2>Output of ".basename((string)__FILE__) . "<hr></h2>";
#######################################################################
It will show the source code and output following.
它将显示以下源代码和输出。
回答by Mike Brant
Typically this is done by showing code within <pre>
or <code>
tags.
通常这是通过在<pre>
或<code>
标签中显示代码来完成的。
回答by Liv
The PHP code will just be a string that you can echo
or print
onto the page, no different than any other data you want PHP to display for you. If you want to keep the formatting (ex. the indentation), put it inside a <pre><code>
block.
PHP 代码只是一个字符串,您可以在页面上echo
或print
在页面上使用它,与您希望 PHP 为您显示的任何其他数据没有什么不同。如果要保留格式(例如缩进),请将其放入<pre><code>
块中。
Ex:
前任:
$php_code = '<?php $foo = bar; ?>';
echo "<pre><code>$php_code</code></pre>";