Read *parsed* PHP file into a variable?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have this strange including problem:  

I want to read a piece of HTML, residing in some file, into a
variable, not echo it out. So far so easy - but now the HTML contains
a line of PHP. And I wish not to get the PHP code into my variable but
the parsed result, just like if the variable was a client ;-). I wish
to keep the nice ?> ...<? HTML area (with highlighting in my editor),
and therefore include etc. don't help because they would echo it out.
heredoc also doesnt help (if this may come up to your mind).

Is there anything I can do...?!! The problem sounds so simple... the
more for such a great tool like PHP ... but... ?!!  

Thanx a lot,  

PS Please post also if you know for sure that there is NO solution to
my problem

Re: Read *parsed* PHP file into a variable?

"Daniel Loose" wrote:

Quoted text here. Click to load it

Easy: $html = file_get_contents(' ');

Take a look here: <

phil [dot] ronan @ virgin [dot] net

Re: Read *parsed* PHP file into a variable?
Quoted text here. Click to load it

Sounds like you could use buffering.

ob_start(); // Start output buffering.
// Instead of outputting anything, the output is now stored in a buffer.
// There, now the file has been included and php parsed.
// It isn't output, it goes into the buffer.
$yourfile = ob_get_clean();
// Now we read everything that has been buffered... And stops buffering.


Result: the file you included has been parsed by the php engine and stored  
inside $yourfil, and nothing was output. Sounds like goal achieved.

SETI @ Home - Donate your cpu's idle time to science.
Further reading at <

Re: Read *parsed* PHP file into a variable?

Quoted text here. Click to load it

Pretty cool, thanx so much! Never heard of output buffering before.

Guess no need to fight with my other Reply anymore (concerning

Enjoy, D.

Re: Read *parsed* PHP file into a variable?

Just discovered and on that url especially the
user contributed function eval_html3(). But can't get it to work.

I made a simple test page:
Perhaps you feel like helping to fix the function. (Or to tell me what
I have misunderstood.) I just renamed eval_html3 to eval_html and
replaced <?php by <?. It's now supposed to work but it doesnt. Here
the test files:

$s = eval_html(file_get_contents('.js.php', 0));
$t = eval_html(file_get_contents('.css.php', 0));

function eval_html($string) {
   $string = '<? ?>'.$string.'<? ?>';
   $string = str_replace( '?>', '', str_replace( array( '<?', '<?' ),
'', preg_replace_callback( "/\?>(.*?)(<\?|<\?)/", "my_eval", $string )
) );
   return eval($string);
function my_eval($arr) {
   return ('echo stripslashes("'.addslashes($arr[0]).'");');

.js.php and .css.php:  
contain plain js/ css plus one line
<? $a = 6+7; echo ".testClass { font-size: ".$a."px; }\n\n"; ?>
or resp.  
<? $a = 4+5; echo 'testVar = '.$a;; ?>
No errors when called directly.

test.php itself does nothing (blank screen), I only want to get away
the errors. You may also download this small test if you want to
figure it out on your own system (See Link)

Thanx a lot! , Daniel

PS Thx to the other poster, will check your idea out soon as possible.

Re: Read *parsed* PHP file into a variable?

Daniel Loose wrote:
Quoted text here. Click to load it

Better have a look at Kimmo's suggestion, because that's the way to go...


Re: Read *parsed* PHP file into a variable?

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Site Timeline