PHP Output Buffer Rocks!

Robert Leitch | 28.08.2007 | Views: 339
If you've ever seen those sites that magically highlight all the search terms you used when you arrive at them from a Google search, then you've probably seen the output buffer at work.

The output buffer is a great thing - it lets you take the whole output of a page and modify it before it reaches the viewer's browser.

Why is that useful? Well, just imagine that you changed the name and URL of a website. Instead of going through every single page and changing every single occurence of the name and URL, you could just add an output buffer handler to the page template (you do have a template, don't you?)

In most cases that would look something like this:

<?php include 'header.php' ?>

Page content

<?php include 'footer.php' ?>

By adding an output buffer function to the header.php file, we can manipulate whatever is in the 'Page content' part. The page content may include static HTML or dynamic content - it doesn't matter where it comes from as it will all go through the output buffer.

The actual nuts and bolts of using the function look like so:

You have a header file that is at the top of every page on the site. Let's assume it looks like this:

<?php
echo '<head><title>Page 1</title></head><body>';
// page content starts here
?>

To get the output buffer working in this example, we need to include the ob_start() function at a point before the page content starts. We also tell the output buffer what to do with the cached page output, by giving it the name of a function to pass the cache to. That is the function that will alter the content of the page.

Let's call our function 'bob'. It can do whatever it wants with the page output, for example:

function bob($text) {

$text=str_replace("all", "everyone", $text);
// replace all occurences of the word "all" in $text with the word "everyone"
}

$text is what the output buffer passes back to the designated function. It can be named anything, because it is automatically sent to the function you specify. For example ob_start('bob') will send the page output to the function bob().

Now we can start putting things together.

Our header.php file now looks like this:


<?php
echo '<html><head><title>Page 1</title></head><body>';

function bob($text) {

$text=str_replace("all", "everyone", $text);
// replace all occurences of the word "all" in $text with the word "everyone"
}

ob_start('bob');

// page content starts here
?>

There's just one more thing required to get this working right - we need to tell the output buffer to stop caching and release the collected output. We can do this in the footer.php file with ob_end_flush(). Otherwise we'll just get a blank page because the script won't know where to release the contents of the buffer.

The footer.php file looks something like:

<?php

ob_end_flush()
;

echo "</body></html>";

?>

If we now make a page called test.php with the following:

<?php include 'header.php' ?>

<i>Hi all, I'm glad to see you all so well.</i>

<?php include 'footer.php' ?>

Then when we view it in the browser, what we should see is:

Hi everyone, I'm glad to see you everyone so well.

There are many wonderful things that can be done with the output buffer, such as keyword highlighting, linking, etc.- even spellchecking, removal of bad language, HTML cleanup... the list goes on!

Published in sections: PHP Coding :

Comments from our visitors:


yjbqewsvduz wrote on 13.09.2008 at 21:04:14:
J5LPba <a href="http://mtqpayvffvmd.com/">mtqpayvffvmd</a>, [url=http://tolzwuquwgfe.com/]tolzwuquwgfe[/url], [link=http://bpgqcfiiswlz.com/]bpgqcfiiswlz[/link], http://sefidtufbykj.com/


uhxmvwxjilq wrote on 11.04.2008 at 08:04:25:
PKK913 <a href="http://jgwpepjuoaim.com/">jgwpepjuoaim</a>, [url=http://otimmxjkmuon.com/]otimmxjkmuon[/url], [link=http://jlufagtklbhe.com/]jlufagtklbhe[/link], http://ogpxlmohkwqf.com/



Add comment:















Add to: del.icio.us   digg   furl   magnolia   reddit   spurl   spurl   Y!

« November 2008 »
Su Mo Tu We Th Fr Sa
            1
2345678
9101112131415
16171819202122
23242526272829
30