tags:

views:

86

answers:

5

Possible Duplicate:
Storing echoed strings in a variable in PHP

Suppose I have

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

print-stuff.php contains PHP/HTML template, which means that when it is included, HTML gets printed. Is there any way to capture that HTML as a string, so that I may save it to use elsewhere?

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

A: 

Have a look at output buffering.

http://us2.php.net/manual/en/function.ob-start.php

Daniel A. White
+2  A: 

You can Output Buffer it to make sure the HTML isn't shown and is instead put into a variable. (PHP will still run, but HTML output will be contained in the variable)

ob_start();
include "print-stuff.php";);
$contents = ob_get_contents();
ob_end_clean();
Chacha102
Could someone state why I'm getting downvotes?
Chacha102
A: 

You can do that if you print in a buffer instead of stdout.

ob_start();
include 'print-stuff.php';
$printedHTML = ob_get_clean();
p4bl0
A: 
$fileStr = file_get_contents('/path/not/url/to/script.php');
karim79
Yes! this is exactly what I wanted.
Steve
A: 

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

But, for the heck of it, I do like this:

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