Save include output to string?

Possible duplicate:
Storing echo strings in a variable in PHP

Let's pretend that

  <? php include "print-stuff.php";  ?> 

print-stuff.php contains a PHP / HTML template, which means that when it is turned on, HTML is printed. Is there a way to capture this HTML as a string so that I can save it for use elsewhere?

Moving the include statement elsewhere is not an option because print-stuff.php also executes the logic (creates / modifies variables) that the surrounding code depends on. I just want to move the output of the file, leaving its logic as it is.

+4
source share
5 answers
$fileStr = file_get_contents('/path/not/url/to/script.php'); 
-4
source

You can Output Buffer to make sure that HTML is not displayed and is instead placed in a variable. (PHP will still work, but the HTML output will be contained in a variable)

 ob_start(); include "print-stuff.php"; $contents = ob_get_contents(); ob_end_clean(); 

....

+22
source

This can be done if you print in the buffer instead of stdout.

 ob_start(); include 'print-stuff.php'; $printedHTML = ob_get_clean(); 
0
source

Honestly, I came here and went ah ha, I know the answer to that !!! Then I looked down and saw that other people got to him before I did it.

But damn it, I like it:

 ob_start(); include 'something.php'; $output = ob_get_contents(); ob_end_clean(); 
0
source

All Articles