PHPackages                             lukaswhite/uk-postcode - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. lukaswhite/uk-postcode

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

lukaswhite/uk-postcode
======================

A PHP class that represents a UK Postcode; for validating, formatting and querying

1.0(7y ago)012.2k↑125%2[1 issues](https://github.com/lukaswhite/uk-postcode/issues)2PHPPHP &gt;=7.0

Since Oct 18Pushed 7y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (2)Used By (2)

UK Postcode
===========

[](#uk-postcode)

[![CircleCI](https://camo.githubusercontent.com/44fbfe1b96adf4ecc25852a0ad7567660f1823f2e76ae3ebeefc391b10c3a75f/68747470733a2f2f636972636c6563692e636f6d2f67682f6c756b617377686974652f756b2d706f7374636f64652e7376673f7374796c653d737667)](https://circleci.com/gh/lukaswhite/uk-postcode)

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
------------

[](#installation)

Via Composer:

```
composer require lukaswhite\uk-postcode
```

### Create an instance

[](#create-an-instance)

```
use Lukaswhite\UkPostcode\UkPostcode;

$postcode = new UkPostcode('sw1a2aa');
```

### Validate a Postcode

[](#validate-a-postcode)

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

Alternatively, use the static method:

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

### Format a Postcode

[](#format-a-postcode)

```
$postcode = new UkPostcode('sw1a2aa');

print $postcode->formatted();

// outputs "SW1A 2AA"
```

### Outcodes

[](#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

[](#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

[](#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

[](#miscellany)

- The class implements the magic `__toString()` method, which will return a formatted version of the postcode.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity56

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

2769d 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 (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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