Page 1 of 1

Hooking <? echo ?> output

Posted: Sat Feb 03, 2007 4:14 am
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

Posted: Sat Feb 03, 2007 4:30 am
by Buddha443556

THanks

Posted: Sat Feb 03, 2007 9:23 am
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]