PHP – RegexRouter

PHP Routing class that translates any valid PCRE into a closure, in 14 lines of wholesome goodness For the impatient: class RegexRouter { private $routes = array(); public function route($pattern, $callback) { $this->routes[$pattern] = $callback; } public function execute($uri) { foreach ($this->routes as $pattern => $callback) { if (preg_match($pattern, $uri, $params) === 1) { array_shift($params); […]

Wrapping / Looping / Infinite Array Traversal

I’ll use JavaScript syntax and provide a JS fiddle POC, but this really applies to almost any language – at least any that use indexed arrays (all that I can think of). To get an index greater than length to “wrap” back from the beginning, use a simple modulus: if ( index >= length ) […]

PHP + MySQL – get table column names

$pdoInstance->query("desc table_name")->fetchAll(PDO::FETCH_COLUMN);

PHP – seriously simple Router

/UPDATE I’ve come to prefer (the very similar) RegexRouter. I’m increasingly uncomfortable with the string replacement in the route method. My upshot of PHP routers (inspired by the various micro-frameworks, like Slim, Silex, F3, Laravel, Tonic, Flight, Klein, etc). class Router { private static $routes = array(); private function __construct() {} private function __clone() {} […]

PHP from command line

This under-appreciated feature has a couple upsides: 1) speed, 2) debug – it prints (or echos) as it processes, so you can easily break or debug to a particular line or, for example, in a loop iteration when running through a mysql resultset. Very simple use as well. On windows, just open cmd and input: […]

JavaScript + PHP + MySQL – determine if user is online or logged in

This is a pretty common task that doesn’t have a perfect answer. It’s not possible to determine if a user disconnects, leaves the page, closes the browser, etc (besides window unload methods, which aren’t practical for this use case). So we fake it as best we can. Here’s how I do it. 1. Create a […]

PHP – sanitize output

Whenever you’re printing user-input to a page – even something as simple as re-populating an input from a previous page via cookie – someone’s bound to try to exploit it. Here’s a simple function to help sanitize user input: function sanitize($html){ if(get_magic_quotes_gpc()){ $html = stripslashes($html); } $html = mb_convert_encoding($html, ‘UTF-8’, ‘UTF-8’); $html = htmlentities($html, ENT_QUOTES, […]

PHP – really simple linkify

we all know that it’s impossible to perfectly match URLs from human input… but i spent a little time compiling various solutions on the boards and have ended up with this (note – this is largely pieced together from other sources – I’m not claiming original work): $html = preg_replace(‘/([a-z]+\:\/\/[a-z0-9\-\.]+\.[a-z]+(:[a-z0-9]*)?\/?([a-z0-9\-\._\:\?\,\’\/\\\+&%\$#\=~])*[^\.\,\)\(\s])/i’, $string); The more difficult part […]

PHP – get tweets

There are a ton of ways to do this, but the one I’ve found that’s consistent and transparent utilizes twitter’s json service. The upshot is this: $user = ‘qozix’; $uri = "{$user}.json?count=10"; $raw = file_get_contents($uri); $data = json_decode($raw); which gives back an array of “tweets” – the text property of each element is the one […]

PHP – Zend PDF Merge

I do a lot of dynamic generation of PDF files as mail attachments, that are never sent to the server as an actual file. I recently had a need to combine them into a single file, which isn’t well documented but is pretty simple – the following function takes an array of Zend_PDF objects and […]