Skip to content
A PHP class that represents a UK Postcode; for validating, formatting and querying
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
src
tests
.gitignore
README.md
composer.json
phpunit.xml.dist

README.md

UK Postcode

CircleCI

A PHP class that represents a UK Postcode.

It allows you to:

  • validate a postcode
  • format a postcode correctly
  • get the outcode, inward code or postcode sector

For example, suppose you request users provide their postcode when filling out their address; there's a very strong possibility they'll enter something like sw1a2aa. This class not only allows you to check that it's a valid UK postcode, but also format it correctly — in this example, that means SW1A 2AA.

Installation

Via Composer:

composer require lukaswhite\uk-postcode

Create an instance

use Lukaswhite\UkPostcode\UkPostcode;

$postcode = new UkPostcode('sw1a2aa');

Validate a Postcode

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

Alternatively, use the static method:

if (UkPostcode::validate('sw1a2aa')) {
	// do something...
}

Format a Postcode

$postcode = new UkPostcode('sw1a2aa');

print $postcode->formatted();

// outputs "SW1A 2AA"

Outcodes

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

$postcode = new UkPostcode('sw1a2aa');
print $postcode->getOutcode();
// outputs "SW1A"

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

$postcode = new UkPostcode('gl91ah');
print $postcode->getOutcode();
// outputs "GL9"

Inward Codes

The inward code is the bit after the outcode. To illustrate:

$postcode = new UkPostcode('sw1a2aa');
print $postcode->getInwardCode();
// outputs "2AA"

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

$postcode = new UkPostcode('gl91ah');
print $postcode->getInwardCode();
// outputs "1AH"

Sectors

The sector is the outcode, followed by the first digit of the inward code. To illustrate:

$postcode = new UkPostcode('sw1a2aa');
print $postcode->getSector();
// outputs "SW1A 2"

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

$postcode = new UkPostcode('gl91ah');
print $postcode->getSector();
// outputs "GL9 1"

Miscellany

  • The class implements the magic __toString() method, which will return a formatted version of the postcode.
You can’t perform that action at this time.