April 28, 2007
Reducing HTML output size 7
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 Raleigh, NC. I'm married to an awesome lady and we have an awesome one year old son. If you'd like to find out more about my fascinating life, check this stuff out:
Track me
Visit my sites
Other stuff
Latest posts
- Google ASCII Art Easter Egg
- Is it so crazy that someone would play VATFUL?
- Now that’s a quesadilla
- Automatically playing audio with HTML5 and Javascript (even on the iPad)
- Prevent console.log() errors
- Tumblin’
- omgtru updates and iPhone web app lessons learned
- Really?
- Easy form hints: HTML5 input placeholders
- Anthem Type
Categories
- apple
- art
- audio
- baseball
- bikes
- books
- browsers
- comedy
- complaints
- crime
- death
- dumb things
- energy
- environment
- family
- fonts
- food
- friends
- gadgets
- games
- henry
- html
- internet
- ipad
- iphone
- itunes
- javascript
- jquery
- life
- los angeles
- marriage
- movies
- msgblorb
- music
- mysql
- new york
- omgtru
- os x
- pasadena
- photos
- php
- plugins
- podcasting
- politics
- portland
- pugs
- rss
- ruby
- splideo
- subversion
- ubuntu
- Uncategorized
- 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.