RE: [PHP4BETA] Grab Output Buffer
| From: | Zeev Suraski | Date: | Wed, 15 Sep 1999 23:01:13 +0000 |
| Subject: | RE: [PHP4BETA] Grab Output Buffer | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-4499@lists.php.net to get a copy of this message | ||
This function is not reentrant, but under standard circumstances, should work fine (four only):
function CaptureOutput($code)
{
ob_start();
eval($code);
$output = ob_get_contents();
ob_end_flush();
return $output;
}
And the usage is:
CaptureOutput("ShowRecord($car)");
Of course, I don't personally recommend this method as it uses eval() and the code has to be encapsulated in quotes (and possibly escaped, and possibly take scope issues into account), but it just comes to show that it's easily possible to implement this using PHP 4.0's technology. A much more typical way of using it would be not through a CaptureOutput() function, but by calling the ob_*() functions directly in the code wherever necessary.
FYI, the ob_*() functions use PHP 4.0's new output layer to implement selective output buffering (ob stand for Output Buffering). They've been available since the first public (and even non-public) beta of PHP 4.0.
Zeev
At 10:05 15/09/1999 , Samuel Liddicott wrote:
On second thougts maybe a rewrite rule with !-f to check for non-existance of the cache, which maps it via a special php3 which performs a virtual/openfile("http://..." on the source php3 and then writes to file and serves. But along the lines of the original question, I have often thought it would be useful to temporarily capture to string or file what would have been written out. e.g. <% function ShowRecord($car) { %> <table><tr><td> ....etc etc <% } %> <% $record=CaptureOutput(ShowRecord($car)); %> Sam -----Original Message----- From: Samuel Liddicott [mailto:sam@campbellsci.co.uk] Sent: Wednesday, September 15, 1999 08:53 AM To: php4beta@lists.php.net Subject: [PHP4BETA] Grab Output Buffer In php4, can a copy of the output buffer be taken, and manipulated, maybe written to file? The reason I ask is this: Spme php3 pages can be lengthy to generate but change rarely. It would be possible to use a prepend to check the age of a cached copy on disk and optionally serve that, or if it is somewhat dated and system load is low, regenerate and serve the page, but then also re-write the cached copy from the buffered output. Sam -- PHP 4 Beta -- PHP 4 Beta-- Zeev Suraski <zeev@zend.com> http://www.zend.com/ For a PGP public key, finger bourbon@netvision.net.il