Having fun with PHP’s output buffer
(Page 2 out of 3)Creating an output "replace function"
Our output replace function will allow us to replace parts of the output later on in the script. This means we can be extremely flexible with the output. The example below demonstrates what I mean:
echo 'I like blue';
// ... do something
// Now it should be 'I like red' instead of blue
// use an output replace function for that
// ... do something more
// And now it should be 'I like yellow' instead of red
// use an output replace function
?>
The above example is only a simple and pointless example, but there are practical situations where a replace function for output would be extremely handy. So let's create that.
The basis behind our function will be the output buffer with a callback. In this callback function, all replaces should be made. Something like this:
function replace_callback($content) {
global $ob_replace;
// Make sure ob_replace is an array
if (!is_array($ob_replace)) { return $content; }
// Do each replace
foreach ($ob_replace as $r) {
$content = str_replace($r['0'], $r['1'], $content);
}
return $content;
}
// Start output buffer with our callback
ob_start('replace_callback');
echo 'I like blue';
?>
Now all we need to write is a simple function that will add our replaces to the global $ob_replace variable, like this:
global $ob_replace;
if (!is_array($ob_replace)) { $ob_replace = array(); }
$ob_replace[] = array($search, $replace);
}
All this function does is makes sure the $ob_replace variable is an array, and adds the replace arguments to it.
Putting both examples together and you get this:
function replace_callback($content) {
global $ob_replace;
// Make sure ob_replace is an array
if (!is_array($ob_replace)) { return $content; }
// Do each replace
foreach ($ob_replace as $r) {
$content = str_replace($r['0'], $r['1'], $content);
}
return $content;
}
function output_replace($search, $replace) {
global $ob_replace;
if (!is_array($ob_replace)) { $ob_replace = array(); }
$ob_replace[] = array($search, $replace);
}
// Start output buffer with our callback
ob_start('replace_callback');
echo 'I like blue';
// Replace with red
output_replace ('blue', 'red');
// Replace with yellow
output_replace ('red', 'yellow');
?>
And that's it! It's now possible to replace parts of the output, after you've printed them.
April 16th, 2006 at 7:52 pm
[…] In this article Dennis Pallett explains what the output buffer is, how to use it, and shares some neat tricks possible with the output buffer. Samples and demo code included. […]
April 17th, 2006 at 9:29 pm
[…] One of the more powerful and handy features that PHP offers is output buffering. It allows you more control over when the client’s browser gets the information instead of just spewing information at random. It is a little tricky to get the hang of, so PHPit.net has put together this new tutorial on how to get started. […]
April 24th, 2006 at 6:26 pm
Hi i’ve heard that its possible to output to the browser while the script is still running(like search query) using some buffer and/or flush function.if u can write or direct me towards such links it would be immensly helpful
May 9th, 2006 at 11:56 pm
Excellent this really helped me understand, the headers all ready sent error is a nightmare when coding in PHP by understanding and using output buffering all my scripts redirect as they should.
Thanks guys.
May 14th, 2006 at 9:03 am
Output buffer compression to save bandwidth is a good option. Nice to see this type of writting in this site.
Regards,
[Rupom]
May 28th, 2006 at 11:04 pm
[…] But with a little help from PHP, we can still create our own HTML tags, and have it working in all browsers. In this tutorial I will show you how. First we’ll go through the basics of creating a new tag, and after that we’ll explore some more advanced functionality. We’ll be making use of an output buffer to create our own tags, so if you’ve never used the output buffer before, it’s probably useful to read the following article first: Having fun with PHP’s output buffer. Let’s get started! […]
July 12th, 2006 at 3:10 am
I have written a few articles about advanced PHP output buffering. Check them out at http://www.briancray.com/blog/view/25/
If you look around my web site, you will find more articles about things to do with it.