Archive for the 'PHP' Category

Robust email address validator - with address suggestions!

I’m sure you’ve seen the simple email address format validation function; they’re usually a simple regular expressing that just check the address portion (the user@example.org bit). That’s really only a bit of the validation that should be done. The RFC822 specs detail that the format of email addresses can be much larger, for example, it could be something like “Andrew Collington & Co.” <a.collington@example.org>, and, of course, the simple regex on that would fail. But even a check on the address format isn’t often enough… The user could enter a correctly formatted email address but simply have mis-spelled the address… they may accidentally type in user@yahooo.com, or user@hitmail.co.uk rather than hotmail.co.uk, and things like that. In which case you may want to check the MX and/or A record to see if its a valid domain. And whilst you’re doing that, why not check to see if it’s a commonly used email host that maybe they’ve typed in wrong?

So here is a class that will allow you to do all that in one easy method call:

Continue reading ‘Robust email address validator - with address suggestions!’

Oracle extension oddity

Today I was working on some sql for Oracle and connecting to the database with the PHP4 OCI extension. I’ve done this many times before, but today ran in to a little oddity that I thought was worth mentioning (mainly so I don’t forget and fall in to the trap again!)…

Continue reading ‘Oracle extension oddity’

Easy chained select lists using Zend Framework and Prototype

Building a set of select lists that are dependant of each other can be a daunting task, but for a simple two-level list - in that what you select from one drop-down will changing what’s displayed in one or more other drop-downs - is actually quite easy thanks to Zend Frameworks and Prototype, both of which support Json.

Continue reading ‘Easy chained select lists using Zend Framework and Prototype’

Create a random thumbnail of a video file

Create a random thumbnail of a video file

Looking at sites like YouTube, you may think it’s quite hard to create a lot of different thumbnails from video files, and have them from random times within that file. But, no, it’s not! As this article shows, by using the very fabulous FFmpeg library, it’s actually a very short amount of code that’s required to create all those lovely random thumbnails.

Continue reading ‘Create a random thumbnail of a video file’

Create a registration key

This simple function allows you to generate a random registration key in the format, '1224-54B1-7D35-5EF7'.

PHP:
  1. function registration_key()
  2. {
  3.     return strtoupper(substr(chunk_split(sprintf('%03d%s',
  4.                       rand(0,999), uniqid('')), 4, '-'), 0, -1));
  5. }

Validate a UK postcode

You've probably seen this before, if not a thousand times, but here's a simple function to check the format of a UK postcode.

PHP:
  1. function valid_uk_postcode($postcode)
  2. {
  3.     return (preg_match('/^([A-PR-UWYZ][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {0,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/i', $postcode)) ?
  4.         true : false;
  5. }

HTTP status codes

Quite often I find myself having to send a HTTP status code in the PHP I'm writing. This typically comes down to usually a 'Forbidden', 'No Content' or a redirect. The trouble is, I usually can't remember the exact code of text used for the slightly more obscure codes, or at least the ones I don't use often... Well, even for the ones I use often, if truth be told!

So I put them all in the following function which you may find useful, too.

Continue reading 'HTTP status codes'

String encoding for HTML

I find this a handy function to encompass a number of different string encoding routines. It allows you to do entity encoding (ÿ), hex (á), html (&aecute;), or url encoding (%E1).

Continue reading 'String encoding for HTML'

CafePress random product update

A small update is required to the CafePress box class - the one that shows a random product from your CafePress store. This is needed because there have been some additional updates to the HTML on the CafePress side, so the regular expression in the class needs updating. It's very easy; just change line 174 for the following:

PHP:
  1. $this->cpPattern = '<a href="/'.$this->storeID.'\.(\d+)"><img .*?alt="([^\"]*)" src="([^"]*)"></a>.*?<a href="/'.$this->storeID.'\.(\d+)">(.*?)</a><br>\$([^<]*)';