PHPackages                             lukaswhite/uk-postcodes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lukaswhite/uk-postcodes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lukaswhite/uk-postcodes
=======================

1.0.0(9y ago)115.5k1PHPPHP &gt;=5.3.0

Since Feb 1Pushed 7y ago1 watchersCompare

[ Source](https://github.com/lukaswhite/uk-postcodes)[ Packagist](https://packagist.org/packages/lukaswhite/uk-postcodes)[ RSS](/packages/lukaswhite-uk-postcodes/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

UK Postcodes
============

[](#uk-postcodes)

> This package has been effectively replaced by [a newer version](https://github.com/lukaswhite/uk-postcode). However, it does **not include** the web service; it's simply a single class.

[![CircleCI](https://camo.githubusercontent.com/892b5c0abfd9cbcfb6156a029866571f73073d44553e67b0f6bb120014a2b508/68747470733a2f2f636972636c6563692e636f6d2f67682f6c756b617377686974652f756b2d706f7374636f6465732e7376673f7374796c653d737667)](https://circleci.com/gh/lukaswhite/uk-postcodes)

This package serves two purposes:

1. A simple class which represents a UK Postcode and contains functionality such as formatting and validation.
2. A wrapper for [this web service](http://www.uk-postcodes.com/api) for geocoding postcodes.

Installation
------------

[](#installation)

Via Composer:

```
"lukaswhite/uk-postcodes" : "dev-master"

```

UkPostcode Class
----------------

[](#ukpostcode-class)

A simple class to encapsulate a UK postcode. While it's primarily used as the return format for the web service, it does have the odd use in itself.

### Constructor

[](#constructor)

```
$postcode = new Lukaswhite\UkPostcodes\UkPostcode('sw1a2aa');

```

### Validation

[](#validation)

```
if ($postcode->isValid()) {
    // do something...
}

```

Alternatively, use the static method:

```
if (Lukaswhite\UkPostcodes\UkPostcode::validate('sw1a2aa')) {
    // do something...
}

```

### Formatting

[](#formatting)

```
$postcode = new Lukaswhite\UkPostcodes\UkPostcode('sw1a2aa');

print $postcode->formatted();

// > "SW1A 2AA"

```

### Outcodes

[](#outcodes)

The outcode is the first part of a UK postcode. To illustrate:

```
$postcode = new Lukaswhite\UkPostcodes\UkPostcode('sw1a2aa');
print $postcode->getOutcode();
// SW1A

$postcode = new Lukaswhite\UkPostcodes\UkPostcode('GL9 1AH');
print $postcode->getOutcode();
// GL9

$postcode = new Lukaswhite\UkPostcodes\UkPostcode('gl91ah');
print $postcode->getOutcode();
// GL9

```

The Web Service Client
----------------------

[](#the-web-service-client)

Provides wrapper for [this web service](http://www.uk-postcodes.com/api) for geocoding postcodes.

### Geocoding a Postcode

[](#geocoding-a-postcode)

It's probably easiest to demonstrate the usage through an example:

```
// Create the client
$client = new Lukaswhite\UkPostcodes\UkPostcodesClient();

// Call the web service
$postcode = $client->postcode('sw1a2aa');

print get_class($postcode);
// Lukaswhite\UkPostcodes\UkPostcode

print $postcode->formatted();
// SW1A 2AA

print get_class($postcode->getCoordinate());
// League\Geotools\Coordinate\Coordinate

print $postcode->getCoordinate()->getLatitude();
// 51.503539898876

print $postcode->getCoordinate()->getLongitude();
// -0.12768084037293

print get_class($postcode->getCoordinate()->getEllipsoid());
// League\Geotools\Coordinate\Ellipsoid

print $postcode->council->title;
// City of Westminster

print $postcode->council->code;
// E09000033

print $postcode->council->uri;
// http://statistics.data.gov.uk/id/statistical-geography/E09000033

print $postcode->ward->title;
// St. James's

print $postcode->constituency->title;
// Cities of London and Westminster

```

If the postcode cannot be found for whatever reason, it'll simply return NULL.

### Nearest Postcode

[](#nearest-postcode)

You can get the nearest postcode to a given point using `getNearest()`.

Specify the point using either an instance of `League\Geotools\Coordinate\Coordinate` or an array of lat / lng, e.g. `[51.503539898876, -0.12768084037293]`.

```
// Create the client
$client = new Lukaswhite\UkPostcodes\UkPostcodesClient();

$to = new League\Geotools\Coordinate\Coordinate([51.503539898876, -0.12768084037293]);

$postcode = $client->getNearest($to);

print $postcode->formatted();

// SW1A 2AA

```

### Postcodes within x miles

[](#postcodes-within-x-miles)

Get the postcodes within x miles of a given point using `getWithin()`. First parameter is a point, second is the number of miles (5 miles maxiumum).

```
// Create the client
$client = new Lukaswhite\UkPostcodes\UkPostcodesClient();

$location = new League\Geotools\Coordinate\Coordinate([51.503539898876, -0.12768084037293]);

$postcodes = $client->getWithin($location, 3);

// returns an array of all postcodes within 3 miles of Lat 51.503539898876, Long -0.12768084037293

```

Testing
-------

[](#testing)

There are a bunch of unit tests (PHPUnit) in `/tests`.

Todo
----

[](#todo)

Better error handling.

Caching.

Unit tests for `getNearest()` and `getWithin()`.

There is a function currently called `getPostTown()` adapted from [this site](http://code.stephenmorley.org/php/handling-uk-postcodes/), but it doesn't appear to be 100% reliable. When I get time, I'll attempt to fix this.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3436d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1bd633ffdf0f66a73a288c4edcacfa43d214cd95a53384c6fe5ea4b3554dd9d?d=identicon)[lukaswhite](/maintainers/lukaswhite)

---

Top Contributors

[![lukaswhite](https://avatars.githubusercontent.com/u/999014?v=4)](https://github.com/lukaswhite "lukaswhite (12 commits)")

### Embed Badge

![Health badge](/badges/lukaswhite-uk-postcodes/health.svg)

```
[![Health](https://phpackages.com/badges/lukaswhite-uk-postcodes/health.svg)](https://phpackages.com/packages/lukaswhite-uk-postcodes)
```

###  Alternatives

[krizon/php-ga-measurement-protocol

Easy use of the Google Analytics Measurement Protocol in PHP

108587.4k5](/packages/krizon-php-ga-measurement-protocol)[fleetbase/fleetops-api

Fleet &amp; Transport Management Extension for Fleetbase

1728.1k5](/packages/fleetbase-fleetops-api)[amplifier/yii-amqp

AMQP adapter for Yii

2237.2k](/packages/amplifier-yii-amqp)[baidubce/bce-sdk-php

BCE SDK for PHP

142.6k2](/packages/baidubce-bce-sdk-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
