简体   繁体   English

如何在页面上显示 PHP 和 HTML 源代码?

[英]How to display PHP & HTML source code on a page?

How would one go about showing PHP code on user end.如何在用户端显示 PHP 代码。 Sort of like w3School does?有点w3School吗?

Having lets say a grey area div, and then showing the code in there without activating it?让我们说一个灰色区域div,然后在那里显示代码而不激活它?

You can use html entities &lt;?php in the html it will be rendered as <?php您可以在 html 中使用 html 实体&lt;?php它将呈现为 <?php

You can use htmlspecialchars to encode your code to use html entities.您可以使用htmlspecialchars对代码进行编码以使用 html 实体。

Use <pre> or <code> tags to wrap your code.使用<pre><code>标签来包装你的代码。

Take a look at http://php.net/manual/en/function.highlight-string.php to 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);
?>

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.'); ?>

The first step is to not wrap that code in PHP tags.第一步是不要将该代码包装在 PHP 标签中。 So instead of this:所以而不是这个:

<?
    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.不是代码本身触发了 PHP 引擎的服务器端编译,而是标记向该引擎指示文件的哪些块是代码,哪些不是。 Anything that's not code is essentially treated as a string and output to the page as-is for the browser to interpret.任何不是代码的内容本质上都被视为字符串并按原样输出到页面以供浏览器解释。

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 式格式:

<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-prettify often comes highly recommended.强烈推荐Google-code-prettify

通常这是通过在<pre><code>标签中显示代码来完成的。

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.它将显示以下源代码和输出。

use the header function of php, this will rea使用php的header函数,这样就可以了

<?php
  header("content-type: text/plain");
?>

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. PHP 代码只是一个您可以在页面上echoprint的字符串,与您希望 PHP 为您显示的任何其他数据没有什么不同。 If you want to keep the formatting (ex. the indentation), put it inside a <pre><code> block.如果您想保留格式(例如缩进),请将其放在<pre><code>块中。

Ex:前任:

$php_code = '<?php $foo = bar; ?>';

echo "<pre><code>$php_code</code></pre>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM