Hooking <? echo ?> output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Hooking <? echo ?> output

Post by mjseaden »

Hi,

I've created a class that generates the XHTML for a webpage. When you call its ->OutputHtml() member function, it echos XHTML to the browser using the PHP 'echo', as you might expect.

I'd like instead to write the contents of the XHTML echoed to a file. Is there any way I can 'grab' or 'hook' the echo(es) from the ->OutputHtml() function and pass them as lines into a file (e.g. grab and then write line-by-line using fwrite(...)).

That way, I could use the class both for browser and file output, instead of needing a different 'mode' for each or different code.

Many thanks

M
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

THanks

Post by mjseaden »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Okay, so the solution is something like.

Code: Select all

<?php
ob_start();

// echo some output
echo 'test string';
echo 'another one';

$handle = fopen( 'test.html', 'w' );

fwrite( $handle, ob_get_contents() );

fclose( $handle );

ob_end_clean();
?>
Thanks very much.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply