ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

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:

function output_replace($search, $replace) {
        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');

?>

(View Live Demo)

And that's it! It's now possible to replace parts of the output, after you've printed them.

« Previous: Introduction & The Basics
Next: Own HTML tags & GZip compression »



7 Responses to “Having fun with PHP’s output buffer”

  1. Piku’s PHP Blog » PHPit: Having fun with PHP’s output buffer Says:

    […] 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. […]

  2. Sam’s random musings » PHPit.net: Having fun with PHP’s output buffer Says:

    […] 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. […]

  3. sam Says:

    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

  4. Andy W Says:

    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.

  5. MA Razzaque Rupom Says:

    Output buffer compression to save bandwidth is a good option. Nice to see this type of writting in this site.
    Regards,
    [Rupom]

  6. PHPit - Totally PHP » Create your own HTML widgets with PHP Says:

    […] 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! […]

  7. Brian Cray Says:

    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.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & The Basics
  2. Output replace function
  3. Own HTML tags & GZip compression
Bookmark Article
Download Article
PDF
Download this article as a PDF file