[thelist] PHP : Redirecting output to a string

R.Livsey R.Livsey at cache-22.co.uk
Wed Nov 27 19:11:01 CST 2002


> Hi...
>
> Is there any way to rewire output so that it goes to a string
> instead of the browser? I have a function that calls some
> include files that are html/php mixed. I use them to display
> the results of a form to the user. What I would like to do is
> use these same functions to generate an HTML email. Is this
> possible? I assume I need to create a string that has all the
> text in it.
>
> Any help would be appreciated.
>
> Jake Aust

Take a look at the 'ob' (output buffering) function.

http://www.php.net/manual/en/function.ob-start.php

Basically before you call the function that would print stuff to the
browser, call ob_start()
Then everything from now on will be buffered, so you call your function
here.
Then once you have finished with that, you can get the contents of the
buffer and assign it to a variable by using ob_get_contents().
Calling ob_end_clean() finishes buffering and clears the buffer.

The code should look something like:

// ----- code start -----
// start buffering
ob_start();

// call your function, would normally output to
// screen but now goes to the buffer
your_function();

// get the buffer and assign it to $buffer.
$buffer = ob_get_contents();

// stop buffering and clean the buffer
ob_end_clean();

// $buffer now contains the contents of what
// would have been sent to the browser.
// ----- code ends -----

hth

--
R.Livsey
Incutio Web Developer
[ PHP | Perl | Java | C# ]
w : www.cache-22.co.uk
  / www.incutio.com




More information about the thelist mailing list