April 28, 2007
Reducing HTML output size
find more posts in: phpI recently found myself writing a small web app for my friend Brian to use on his cell phone, one of the last places where page size really matters. Since every character counts, we want to keep our page structure lean and mean (probably a good idea on a mobile device anyway). But after that there’s a lazy, completely pain-free step we can take: stripping all of the redundant whitespace from our HTML.
With PHP’s output buffer handlers we can just set it and forget it:
function clean_html($output){ $output = preg_replace("/\n/", "", $output); $output = preg_replace("/\t/", "", $output); $output = preg_replace("/\s+/", " ", $output); return $output; } ob_start('clean_html'); //a bunch of stuff that prints out html ob_flush();
This will give use one long, ugly line of dense markup, but what do we care? It’s still valid and the browser handles it just fine. This seems to reduce page size by about 10-20% in this particular application.
7 Responses
Leave a Reply
My name is Joey Nelson. I'm a web developer living in Portland, OR. I'm married to an awesome lady and we have two weird little pugs. I do a weekly comedy podcast and make dumb songs with this guy Ben. I don't post to this blog very often, and very few people actually read it. If you'd like to find out more about my fascinating life, check this stuff out:
Stuff
Visit my sites
Visit my friends
Latest posts
Categories
- art
- audio
- baseball
- bikes
- books
- browsers
- complaints
- crime
- death
- dumb things
- energy
- environment
- family
- friends
- gadgets
- internet
- life
- los angeles
- marriage
- movies
- music
- mysql
- new york
- os x
- pasadena
- photos
- php
- plugins
- podcasting
- politics
- portland
- pugs
- rss
- ruby
- splideo
- subversion
- ubuntu
- video
- watir
- wordpress
- work
i’d like to use this some day. perhaps when i start caring about other people’s cell billz. lolz u no??
jase you should be worried about your mom’s cell bill, she’s callin me like 24/7
she has free national calling it’s all good. also, a raging yeast infection.
oh she might want to get a hepatitis test too, keep it on the dl.
GAY
WHAT
DEY TOOK MY SON. OVER WHAT? SOME MUSIC? IT HURTS, IN MI CHEST.