FindMyNearest is a PHP class for working with UK postcodes, and in particular for finding the distance between two postcodes. It was originally written as part of a project to help people find a local tree surgeon, hence the name, and is also at the heart of

FindMyNearest can be used with a number of web services providing data on UK postcodes, such as It also supports storing data locally in either MySQL or a delimited text file.

Using FindMyNearest

FindMyNearest comes with a number of drivers for accessing data in different formats. This examle shows the use of findMyNearest with

require 'path/to/FindMyNearest.php';

// cachefile must be readable and writeable
// please set cachettl to at least 604800 seconds (one week)
$fmn FindMyNearest::factory('geopostcode', array(
'cachefile' => 'path/to/cachefile',
'cachettl' => 604800)

// loaddata must always be called before any method
// that needs to look up data
if (! $fmn->loaddata()) {
"Error loading data: " $fmn->lasterror() . "\n";

// Check if we know about a code. Returns the best match, or false if no match
$goodcode $fmn->validcode('SW1A 1AA');    

// calculate distance between two codes
$distance $fmn->calc_distance('SW1A 1AA''CV32 5HZ'); 

if (
$distance === false) {
"There was an error " $fmn->lasterror();

// 'normalise' a postcode
$normalised $fmn->normalise('sw1A1aA');
// $normalised is now set to SW1A 1AA

// Are two postcodes the same?
$same $fmn->samepostcode('SW1A 1AA''sw1A1aA');

More documentation is available in FindMyNearest's README file or on the FindMyNearest wiki.

Obtaining FindMyNearest

Two versions of FindMyNearest are made available from The standard version includes an SQL file to create a local MySQL table containing data from Ordnance Survey's Code Point Open. This makes it a big file. We also distribute a version without the SQL file. If you plan to use FindMyNearest with or another web service you won't need the SQL file.

FindMyNearest is developed on Git-Hub, from where you can always get the latest source: FindMyNearest on Git-hub.


findMyNearest is released under the GNU General Public License.