- Web Design Tutorials, Tips, Top Resources

HTML Optimization: Crunch Your HTML with PHP

by Shirley Kaiser, SKDesigns

Published January 26, 2008. Copyright © 2008 Shirley E. Kaiser, M.A., SKDesigns and All rights reserved.

The PHP code below is an example of how to dynamically optimize your HTML server-side using PHP. This example uses the 'stripwhitespace' and 'stripBuffer' functions to remove extraneous whitespace (carriage returns, extra spaces).

This approach allows you to work with your HTMl markup formatted as you wish locally but automatically (dynamically) serve an optimized version to your site visitors - an ideal approach for many who prefer to work with multi-line HTML, those who don't want to manually add and remove extraneous whitespace whenever the HTML or content needs tweaking or updating, or all the above.

You are welcome to use the example PHP code shown below if desired. Note that the example below is fairly generic and you may need to add more rules or change some of the rules for your particular site to dynamically remove every single instance. This PHP code example is provided with the assumption that whoever uses it is already familiar with PHP.

Of course, in order for this to work, your server must be configured to use PHP, and you must be able to use PHP within your HTML files.

/* ---------------------------------
26 January, 2008 - 2:55pm:
<!-- -->
Adapted for by Shirley Kaiser, SKDesigns

The example below is adapted from a post by londrum 8:29 pm on June 7, 2007:
<!-- -->
"crunch up your HTML into a single line
a handy little script..."

This PHP code goes at the very TOP of the PHP-enabled HTML webpage
above EVERYTHING else. Recommendation: use a PHP include file for this
to have only one file to maintain.
--------------------------------- */
function stripwhitespace($bff){

/* carriage returns, new lines */
/* tabs */
/* opening HTML tags */
$bff=str_replace(array(">\r<a",">\r <a",">\r\r <a","> \r<a",">\n<a","> \n<a","> \n<a",">\n\n <a"),"><a",$bff);
$bff=str_replace(array(">\r<d",">\n<d","> \n<d",">\n <d",">\r <d",">\n\n<d"),"><d",$bff);
$bff=str_replace(array(">\r<f",">\n<f",">\n <f"),"><f",$bff);
$bff=str_replace(array(">\r<h",">\n<h",">\t<h","> \n\n<h"),"><h",$bff);
$bff=str_replace(array(">\r<i",">\n<i",">\n <i"),"><i",$bff);
$bff=str_replace(array(">\r<l","> \r<l",">\n<l","> \n<l",">  \n<l","/>\n<l","/>\r<l"),"><l",$bff);
$bff=str_replace(array(">\r<p",">\n<p",">\n\n<p","> \n<p","> \n <p"),"><p",$bff);
/* closing HTML tags */
$bff=str_replace(array(">\r</d",">\n</d",">\n </d"),"></d",$bff);
/* other */
$bff=str_replace(array("\n<div")," <div",$bff);
$bff=str_replace(array(">\r\r \r<"),"><",$bff);
$bff=str_replace(array("> \n \n <"),"><",$bff);
$bff=ereg_replace(" {2,}",' ',$bff);
$bff=ereg_replace("  {3,}",'  ',$bff);
$bff=str_replace("> <","><",$bff);
$bff=str_replace("  <","<",$bff);
/* non-breaking spaces */
$bff=str_replace(" &nbsp;","&nbsp;",$bff);
$bff=str_replace("&nbsp; ","&nbsp;",$bff);
/* Example of EXCEPTIONS where I want the space to remain
between two form buttons at */
/* <!-- --> */
/* name="select" /> <input */
$bff=str_replace(array("name=\"select\" /><input"),"name=\"select\" /> <input",$bff);


About the Author

 Deliver First Class Web Sites: 101 Essential Checklists, by Shirley Kaiser. SitePoint (07-2006).  Shirley Kaiser owns SKDesigns, a Web site design and development business she started in 1996. She specializes in accessibility-friendly, user-centered Web site design and graphics, Web standards, information architecture, Web site speed optimization, and collaborative team projects. Shirley writes weekly columns, has authored dozens of tutorials and articles related to graphics, Web site design, and the Internet, and writes a blog on Web design topics, Brainstorms and Raves. Shirley is also the editor and owner of, a popular and valuable educational resource devoted to website owners, designers, and educators. In addition, Shirley is the author of Deliver First Class Web Sites: 101 Essential Checklists, published by SitePoint Pty. Ltd., July 2006.

Related Optimization Articles, Tutorials, Code Examples

Related within Articles, Tutorials

Related within Web Site Resources's section of annotated links to many top-notch tutorials, tips, recommended books, and more.

top‘On this page’ menuNavigation belowSearch/Sidebar

Articles, Tutorials, Tips:

Most Popular Books

In association with  In Association with 

Our Other Sites


Effective, accessible, standards-based Web site design since 1996

SKDesigns -

Brainstorms & Raves

Weblog on Web design, CSS, standards, music, more. By Shirley Kaiser, SKDesigns.

Brainstorms & Raves

Within Articles, Tutorials, Tips:

Within Web Site Resources:
SkipShow or Hide Detailed Navigation