Category Archives: PHP

Regex – match anything but a string

Regular expressions are something of a black art for programmers, and many get by without ever really learning them properly. Sure, most people will be able to use them to some level, but if you don’t use them for complex tasks in your day to day work you’ll never really need their advanced functionality. I

PHP Spinner Updated – Spin articles for SEO

Quite a while ago I wrote a post on spinning text using PHP to create lots of fresh content from one single block of marked up text. I actually had use for a nested version of this spinning code the other day so set about creating a more efficient version of my previous nested spin

Hierarchical Class Loading With Namespaces

A challenge often encountered when developing in PHP is the ability to instantiate a class from the highest point in a folder hierarchy where that class exists, but fall down the hierarchy when it is missing. This can be done using a factory class and reflection, but is not the most obvious thing in the world!

Optimising Your Site’s CSS, JavaScript, Images and Server Settings

The speed at which a site loads these days is becoming less of an issue, but still I occasionally visit a site where I think “wow, that’s slow!” and look at the requests it is making in Firebug to see what on earth is going on. Often it’s a simple case of loads and loads of individual requests being made for js files, css files and (large) images. We can do something about this!

Using Google to Calculate Driving Distance / Time in PHP

A common problem for programmers is to calculate the straight line distance between two points given coordinates (lon / lat or easting / northing) and is trivial to solve. A less simple task is calculating the road distance between two addresses / postcodes / sets of coordinates.

But, as usual, Google can do this with ease and while it does not allow this functionality for every country in it’s API it is very simple to scrape the required information from their html service that they provide to everyone.

Dynamically Named Static Method Calls

Something that is being added in PHP 5.3.0 but is sorely missed in current versions is the ability to call a static class method from variables. It is something that is occasionally very useful (e.g. you have a validation class triggering other regional classes) and is already possible with standard class calls. Thankfully it’s very easy to simulate with eval…

Asynchronous cURL Requests

A possibly underused technique available to PHP developers is the ability to spawn a background PHP script without JavaScript. All you need to do this is the cURL library and a few lines of code. Let me explain, suppose you have a script that is going to take minutes (or longer) to run, you don’t want to sit looking at a blank screen, and your users certainly won’t! In this kind of scenario you can separate out that logic to a background file and leave your view free for other things, perhaps to query the database to see how the background process is doing?

Spin Text For SEO – A PHP Spinner

I recently had an SEO expert give us a few hours of his time to provide some suggestions to compliment our SEO strategy. One of the techniques that he introduced I was so impressed with (due to it’s utter simplicity) that I am kicking myself for not thinking of it before! Basically, if you have ‘doorway’ pages into your site (e.g. you have pages for ‘Cambridge Widgets’ and ‘Preston Widgets’ and alike) and want them to be dynamically generated, from the same content, but not to suffer horrible duplicate content penalties, you can use a ‘spinning’ function to generate contextually similar, different, content. The original concept is here, but I have enhanced it a bit to better suit my needs.

Validate E-mail Addresses

I know this is a topic with 1001 solutions, but I still get asked for an e-mail validation check from time to time, so here is one that I have used in the past. It’s a cut above average as it allows very basic validation, confirmation of a valid domain ending or even performs an MX record check to ensure that a mail server is at least set up for the domain in question.

Convert URLs to Links in a String

Another common function that I get asked for is the ability to highlight links in a block of text that contains urls, but doesn’t have them within anchor tags. So, to stop me hearing this question and advising people to ‘use Google’ here is a function that will do this. It very likely isn’t bullet proof (and isn’t the most elegant thing I’ve ever seen), but will hopefully be either good enough or provide inspiration for your own solution.

PHP Function to Apply Multiple Others…

From time to time I’ll post little snippets of code that I have been asked for, or see requested again and again. One such request is the ability to apply a set of transformations to text (or whatever) again and again without writing them out each time.

Removing WordPress Autosave and Drafts

Another problem with Wordpress that has almost instantly begun to bug me is the autosave ‘feature’ of Wordpress. I simply don’t want it on! There doesn’t seem to be an option, and despite downloading a plugin to fix it ‘Disable Revisions and Autosave’ (by Exper) this doesn’t seem to work anymore in WP 2.8, damn.