3

I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).

I tried using $html = file_get_content("B590.php");

But then $html will have the content of B90.php as plain text(with php code).

Is there any method where I can get the content of the file after it has been evaluated? There seems to be many related questions like this one and this one but none seems to have any definite answer.

Community
  • 1
  • 1
antnewbee
  • 1,779
  • 4
  • 25
  • 38

5 Answers5

5

You can use include() to execute the PHP file and output buffering to capture its output:

ob_start();
include('B590.php');
$content = ob_get_clean();
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
3
    function get_include_contents($filename){
      if(is_file($filename)){
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
      }
      return false;
    }

    $html = get_include_contents("/playbooks/html_pdf/B580.php");

This answer was originally posted on Stackoverflow

antnewbee
  • 1,779
  • 4
  • 25
  • 38
  • Do you have a reference to the "original" post - I found this extremely useful and would like to share the love :) – kwah Aug 16 '13 at 17:48
  • 1
    dont actually remember...but i think it was this http://stackoverflow.com/questions/6688343/php-mailer-and-html-includes-with-php-variables – antnewbee Aug 30 '13 at 06:23
1

If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:

ob_start();
include('B590.php');
$html = ob_get_clean();

Example:

B590.php

<div><?php echo 'Foobar'; ?></div>

current.php

$stuff = 'do stuff here';
echo $stuff;
include('B590.php');

will output:

do stuff here
<div>Foobar</div>

Whereas, if current.php looks like this:

$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;

The output will be:

do stuff here
Some more
<div>Foobar</div>

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
1

To store evaluated result into some variable, try this:

ob_start();
include("B590.php");
$html = ob_get_clean();
a.ilic
  • 354
  • 3
  • 11
0
$filename = 'B590.php';
$content = '';

if (php_check_syntax($filename)) {
    ob_start();
    include($filename);
    $content = ob_get_clean();
    ob_end_clean();
}

echo $content;
Alastor
  • 357
  • 2
  • 4
  • 11
  • 1
    *For technical reasons, this function is deprecated and removed from PHP.* – ThiefMaster Oct 23 '12 at 10:40
  • Reference for removal from PHP ("5.0.5 This function was removed from PHP. "): http://php.net/manual/en/function.php-check-syntax.php – kwah Aug 16 '13 at 17:50