PHP – RegexRouter

PHP Routing class that translates any valid PCRE into a closure, in 14 lines of wholesome goodness For the impatient:

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:

To get a negative index […]

PHP + MySQL – get table column names

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).


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:

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):

The more difficult part was differentiating what […]

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:

which gives back an array of “tweets” – the text property of each element is the one with the actual string of the tweet. so if you wanted […]

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 […]