4 Fantastic PHP Output Buffering Tricks for Web Developers

Tuesday, April 21st, 2009 Tags:

If you've read my Dev Tips article Output Buffering for Web Developers, a Beginner's Guide you know that PHP output buffering is useful and awesome at the same time.

To recap the article, output buffering puts your PHP script's output in a buffer instead of sending it directly to the browser in pieces, allowing you to manipulate your webpage as a whole before the user sees it. That awesomeness is what we'll take advantage of now.

To prevent reinventing the wheel, we'll reuse the code we wrote in Output Buffering for Web Developers, a Beginner's Guide:

<?php
// start output buffering at the top of our script with this simple command
// we've added "ob_postprocess" (our custom post processing function) as a parameter of ob_start
ob_start('ob_postprocess');
?>

<html>
<body>
<p>Hello world!</p>
</body>
</html>

<?php
// end output buffering and send our HTML to the browser as a whole
ob_end_flush();

// ob_postprocess is our custom post processing function
function ob_postprocess($buffer)
{
	// do a fun quick change to our HTML before it is sent to the browser	return $buffer;
}
?>

Specifically, we’ll use the ob_postprocess($buffer) function for each tip.

1. Optimize HTML by removing repeating spaces

Repeated spaces don't render in a browser, but your end-user still has to download all the extra space characters as bytes. Let's turn repeating spaces into only one space.

function ob_postprocess($buffer)
{
	$buffer = trim(preg_replace('/\s+/', ' ', $buffer));
	return $buffer;
}

2. Compress HTML with gzip

Compressing your output with gzip is a highly recommended practice by anybody who deals with website performance, including Yahoo!.

function ob_postprocess($buffer)
{
	// check if the browser accepts gzip encoding. Most do, but just in case
	if(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
	{
		$buffer = gzencode($buffer);
		header('Content-Encoding: gzip');
	}
	return $buffer;
}

3. Add acronyms to HTML

Adding the &lt;acronym> tag all over your page can be a pain in the butt. Luckily, we can use PHP output buffering to do it for us.

function ob_postprocess($buffer)
{
	$acronyms['html'] = 'hypertext markup language';
	$acronyms['css'] = 'cascading style sheets';
	foreach($acronyms as $acronym => $meaning)
	{
		$buffer = preg_replace('/(\b' . $acronym . '\b)(?=[^>]*<)/i', '<acronym title="' . $meaning . '">\\1</acronym>', $buffer);
	}
	return $buffer;
}

4. Encode characters as HTML entities

Are you using styled quotes and ellipses? Are you using &amp; instead of a raw ampersand? Use PHP output buffering to do the heavy lifting for you.

function ob_postprocess($buffer)
{
	// add styled double quotes
	$buffer = preg_replace('/"(?=[^>]*<)([^"]*)"(?=[^>]*<)/u', '&#8220;\\1&#8221;', $buffer);
	// add styled apostrophes
	$buffer = preg_replace("/'(?=[^>]*<)/i", "&#8217;", $buffer);
	// add ellipses
	$buffer = str_replace('...', '&#8230;', $buffer);
	// encode ampersands
	$buffer = str_replace('&', ' &amp; ', $buffer);
	return $buffer;
}

That's all I'm going to give you for now. I can't give you all of my secrets in one post!

Enjoy fooling around with the buffer in a socially acceptable way, and let us know if you find anything too cool to keep to yourself.

Tags:

If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Brian Cray is a marketing & Internet strategy consultant, web developer, and Apple fanatic living in Columbus, Ohio, USA. His work has been featured by Mashable, NETTUTS, Smashing Magazine, and many others. He's happy to share his knowledge with you on DevTips and his own blog at Brian Cray.com.
  1. April 21, 2009 at 3:58 pm
    • April 21, 2009 at 4:15 pm
  2. Paul
    April 21, 2009 at 4:01 pm
  3. April 21, 2009 at 4:08 pm
    • Paul
      April 21, 2009 at 4:47 pm
      • April 22, 2009 at 5:18 am
  4. April 21, 2009 at 4:15 pm
  5. April 22, 2009 at 6:10 am
    • April 22, 2009 at 12:31 pm
  6. Brady
    April 23, 2009 at 12:35 pm
    • Brady
      April 23, 2009 at 12:36 pm
      • April 23, 2009 at 1:54 pm
      • April 23, 2009 at 2:47 pm
  7. mightyuhu
    May 7, 2009 at 4:45 am
  8. May 13, 2009 at 7:36 pm
    • May 14, 2009 at 5:29 pm
  9. July 30, 2009 at 3:52 am
  10. joel
    August 13, 2009 at 5:33 am
  11. joel
    August 13, 2009 at 5:35 am
  12. August 13, 2009 at 2:19 pm
  13. JeffM
    August 28, 2009 at 2:46 am
    • mark
      September 1, 2009 at 3:07 pm
      • JeffM
        September 3, 2009 at 9:46 am
  14. JeffM
    August 28, 2009 at 3:34 am
  15. Craig
    October 13, 2009 at 11:50 pm
  16. February 3, 2010 at 10:53 am
  17. July 30, 2010 at 2:25 am
  18. July 30, 2010 at 2:27 am
  19. Stalingrad
    August 4, 2010 at 8:39 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates