简体   繁体   English

如何在浏览器中打印php文件(源代码)?

[英]How can I print a php file (source) in the browser?

How can I print a php file (source) in the browser without the php tags at the start and at the end? 如何在浏览器中打印PHP文件(源代码),而在开头和结尾都没有php标签?

So if my php file looks like this: 因此,如果我的php文件如下所示:

<?php
  if( true ){ 
    echo 'hello world';
  }
?>

And in another file I want to load that file and echo the if statement so from the line 2 to 4, but not loosing the tabbing. 在另一个文件中,我想加载该文件并在第2行至第4行中回显if语句,但不要丢失制表符。

I have tried: fgets() and file_get_content() functions, butI can only echo an non-tabbed data. 我试过了: fgets()file_get_content()函数,但是我只能回显未制表的数据。

Is there a method for this problem? 有解决这个问题的方法吗? or I have to write code for tabbing the source? 还是我必须编写代码来查看源代码?

This should work for you: 这应该为您工作:

(Here I get all lines into an array with file() . Then I cut out the first and the last line with array_slice() . After this I simply loop through the array an print it) (在这里,我使用file()将所有行放入一个数组中。然后,使用array_slice()剪切出第一行和最后一行。在此之后,我简单地遍历该数组进行打印)

<?php

    $lines = file("file.php");
    $lines = array_slice($lines, 1, count($lines)-2);

    foreach($lines as $line)
        echo str_replace(" ", "&nbsp;", $line) . "<br>";

?>

output: 输出:

  if( true ){  
    echo 'hello world'; 
  } 

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

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