PHP: Capture Included File Contents to Variable

I banged my head against several walls on this a while back and finally gave up.  Today I found this post over at bin-co.com describing how to capture the contents of an include file to a variable.  Here’s the code…thanks Binny!

|

The code:

$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;
}
Comments are closed.

Powered by WordPress. Designed by Woo Themes