Capture the Output of an Included File

It is possible to capture the output of an include to a variable. This is useful for caching and code generation. I found this feature when I was creating a code generation script for IFrame.

$string = getIncludeContents('template.php');

function getIncludeContents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}

More on include()

Comments

Bill Hogsett at 20 Feb, 2009 02:38
Thanks for sharing a neat and useful function. I found the post with a Google search of "php capture output". I am using it on my weather pages.

Bill
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed