Use your left/right keys to browse tutorials
Compress your HTML code using PHP

Compress your HTML code using PHP

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on April 2, 2009

When we write code we love to format it nicely adding tabs, line breaks and indentations, but the end user isn’t interested in how lovely the source code is, they just want the page content, so this script strips out all the line breaks and spaces in your code and puts it on one line, compressing your code and making it faster.

	//  start output buffer

<!-- all xhtml content here -->

	// end output buffer and echo the page content

	//  this function gets rid of tabs, line breaks, and white space
	function compress_html($compress)
		$i = array('/>[^S ]+/s','/[^S ]+</s','/(s)+/s');
		$ii = array('>','<','1');
		return preg_replace($i, $ii, $compress);

More tutorials from Papermashup
11 discussions around Compress your HTML code using PHP
  1. whois says:

    This is very helpful!

  2. Doesn’t work. It breaks simple HTML5 code… (PHP5.2, UTF-8)

  3. Roger says:

    I hate to be a kill joy, but this isn’t technically “compressing”.
    GZip, Deflate, ZLib etc. – That is Compression.

    This is simply stripping unwanted chars.

  4. Balaji J H says:

    Whether i need to add the html codes instead of


  5. Ian says:

    This script doesn’t seem to work for me. I’ve literally followed instructions and placed the top code at the very top of the .php page and the bottom at the very bottom with no space and HTML in between. However on output all comments, line breaks and white space still remain. Is there something else I’m missing?

  6. Ian says:

    I was actually looking to figure out a way to do something similar to this. Ideally I just wanted a php script which would scan through the html doc and remove all HTML comments. I use a lot of comments whilst in development but don’t necessarily want these to go live and the manual way of stripping them out is a bit of a chore.

    Any chance you could show how I can alter the script to do this?

  7. Ryan Maughan says:

    Where do you call the function?
    All it’s doing, is starting the buffer, echoing the content, then ending the buffer.


    • Ashley says:

      Hey Ryan, yeah your right however its stripping out all the white space in your code when it renders the page therefore compressing your code down to one line. you don’t call the function. You simply wrap your HTML code in the PHP buffer.

  8. Jutta says:

    i dont anderstand where to put this php code.
    In the function,head,index?
    In the body tag?
    Will be great if you will explain it to me.

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.

What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.