0

How can I print a php file (source) in the browser without the php tags at the start and at the end?

So if my php file looks like this:

<?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.

I have tried: fgets() and file_get_content() functions, butI can only echo an non-tabbed data.

Is there a method for this problem? or I have to write code for tabbing the source?

3
  • You need to put tabs in the output that you echo if you want tabs in the output
    – Mark Baker
    Apr 20, 2015 at 15:11
  • 1
    Please include your attempts, no matter if they worked! Just show your effort and work
    – Rizier123
    Apr 20, 2015 at 15:24
  • So where are we with this question?
    – Rizier123
    Apr 20, 2015 at 17:28

1 Answer 1

1

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)

<?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'; 
  } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.