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 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)) {
        include $filename;
        $contents = ob_get_contents();
        return $contents;
    return false;
Comments are closed.

Powered by WordPress. Designed by Woo Themes