Papermashup

Subscribe


Tweets


"RT @Eva_Shaughnessy: #finedining @romomobilecafes restaurant this evening with #NewHair #winning #SaturdayNight #GirlsNightOut http://t.co/…"

@ashleyford 3 weeks ago

"RT @kycutwilson: @ashleyford @burgerbeartom incredible. There's 5 more left! Shout about it!!"

@ashleyford 1 month ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Compress your HTML code using PHP

AshleyAshley

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.




<?php
	//  start output buffer
	ob_start('compress_html');
?>

<!-- all xhtml content here -->

<?php
	// end output buffer and echo the page content
	ob_end_flush();

	//  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);
	}
?>

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 11
  • whois
    Posted on

    whois whois

    Reply Author

    This is very helpful!


  • Mike Edward Moras (e-sushi™)
    Posted on

    Mike Edward Moras (e-sushi™) Mike Edward Moras (e-sushi™)

    Reply Author

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


  • Roger
    Posted on

    Roger Roger

    Reply Author

    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.


  • Balaji J H
    Posted on

    Balaji J H Balaji J H

    Reply Author

    Hi,
    Whether i need to add the html codes instead of

    Right???


  • Ian
    Posted on

    Ian Ian

    Reply Author

    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?


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @Ian Sorry my mistake there was an error in the code! should work fine now, let me know how you get on


  • Ian
    Posted on

    Ian Ian

    Reply Author

    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?


  • Ryan Maughan
    Posted on

    Ryan Maughan Ryan Maughan

    Reply Author

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

    Cheers,
    Ryan.


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      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.


  • Jutta
    Posted on

    Jutta Jutta

    Reply Author

    Hello,
    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.
    Thanks
    Jutta


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @jutta You need to add the code at the top of your page before any other code