UK Postcode

Posted on 2006-06-06
Last Modified: 2012-08-14
I have a list of UK Postcode that I need to check for the correct format.  

Users address are entered like: $address = "street, town, city, county, postcode";

I split these up into parts like so: $part = explode(',',$Address); which gives me:

$part[0] = 'street';
$part[1] = 'town';
$part[2] = 'city';
$part[3] = 'county';
$part[4] = 'postcode';

But some users enter their phone number instead of the postcode which gives me a pain in the head...

So i'd like to check the last part in the string so I can either put the value in a $telephone = '0123456789'; or $part[4] = 'AB10 1LB';

So how would i check the format of a Postcode? example: AB10 1LB

thank you
Question by:ellandrd
    1 Comment
    LVL 49

    Accepted Solution


    if(preg_match('/^[a-z]+\d+\s+\d+[a-z]+$/i', $part[4])) {
    } else {


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now