Category archive: php
WordPress plugin: x-y of z 1
find more posts in: php, wordpressYou can find it on the plugins download page now. This basically just gives you a function that will print out a snippet of text like “Viewing 1-10 of 423 posts” based on the contents of the page. Hopefully this is marginally useful to someone out there.
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.
PHP relative time function 0
find more posts in: phpI couldn’t find a PHP function that would take a number of seconds as input and give me something like “3 weeks and 2 days” so I put this together:
function rel_time($seconds){ if($seconds < 60){ if($seconds < 0){ $seconds = 0; } switch($seconds){ case 1: return "1 second"; break; default: return "$seconds seconds"; break; } }else{ $date_push = array(); $time_units = array( 'year' => (365*24*60*60), 'month' => (30*24*60*60), 'week' => (7*24*60*60), 'day' => (24*60*60), 'hour' => (60*60), 'minute' => (60)); foreach($time_units as $unit=>$unit_time){ $total = 0; if($unit=='day' && count($date_push) && ($seconds < $time_units['day'])){ $seconds = 0; } while($seconds >= $unit_time){ $seconds -= $unit_time; $total++; } switch ($total){ case 0: break; case 1: $date_push[] = "1 $unit"; break; default: $date_push[] = "$total {$unit}s"; break; } if(count($date_push) == 2){ break; } } return implode(" and ", $date_push); } }
Example usage:
$rel_time = rel_time(time() - $timestamp); print "Posted $rel_time ago";
It’s not as efficient as it could be, but it gets the job done for now. And it’s pretty easy to configure it for the time units that you want to use.
Snap Preview Anywhere contest 0
find more posts in: php, workI’ve spent the past couple of weeks at work building a web application for Snap’s latest contest and it just went live at contest.snap.com. It allows users to submit ideas (for new features for a product) and lets them do some simple Digg-style voting.
The concept is very similar to the last Snap contest but it’s now a standalone web application (instead of a half dozen hacky Wordpress plugins). The site is actually a heavily-modified version of my message board software that runs omgtru.com.
GD TTF PCDTR in PHP 2
find more posts in: internet, php, ubuntuI’d been having a lot of trouble getting my Ubuntu machine to render TTFs with GD in PHP. So much trouble that I’d even tried crazy things like recompiling PHP. Luckily I found this post on excitris.com and I was able to solve all of my problems with one line of code. Since the code on their site is completely mangled, here it is:
putenv('GDFONTPATH=' . realpath('.'));
Now I can use dynamic text replacement without going insane.
- 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