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.

1
  • 1
    Shouldn't it be file_get_contents('B590.php');, with an s at the end? probably a typo, but still... Commented Oct 23, 2012 at 10:41

5 Answers 5

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();
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Do you have a reference to the "original" post - I found this extremely useful and would like to share the love :)
dont actually remember...but i think it was this stackoverflow.com/questions/6688343/…
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>

Comments

1

To store evaluated result into some variable, try this:

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

Comments

0
$filename = 'B590.php';
$content = '';

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

echo $content;

2 Comments

For technical reasons, this function is deprecated and removed from PHP.
Reference for removal from PHP ("5.0.5 This function was removed from PHP. "): php.net/manual/en/function.php-check-syntax.php

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.