Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly match UK postcodes by prefix?

I have a number of restaurants who all deliver to certain postcode areas in London, for example:

  • EC1
  • WC1
  • WC2
  • W1

When someone searches for a restaurant that delivers to their home, they enter their full postcode.

Some people enter the postcode correctly with the space, some of them just enter all letters and numbers attached, without a space separator. To harmonize things, I remove any space in the postcode before attempting a match.

So far, I used to match the postcode to the prefixes by just checking if it starts with the prefix in question, but then I realized that this is not foolproof:

  • WC1E123 => correct match for WC1
  • W1ABC => correct match for W1
  • W10ABC => incorrect match for W1, should only match the W10 prefix

How can I know, given a full postcode with no space, if it matches a given prefix, while not failing the W1 / W10 test above?

Is there any solution at all to the problem, that would not involve forcing the customer to enter the postcode with the space at the correct position?

like image 658
BenMorel Avatar asked Dec 01 '22 02:12

BenMorel


2 Answers

There are 6 possible formats for postcodes in the UK:

  • A9 9AA
  • A9A 9AA
  • A99 9AA
  • AA9 9AA
  • AA9A 9AA
  • AA99 9AA

I think there need to be two parts to your solution. The first is to validate the input; the second is to grab that first part.

Validation

This is really important, even though I realise you have said this is not what you are trying to do, but without it you are going to struggle to get the right prefix and possibly send your drivers to the wrong place!

There are a couple of ways you can do it, either use a 3rd party to help you capture a complete & correct address (many available including http://www.qas.co.uk/knowledge-centre/product-information/address-postcode-finder.htm (my company)), or at a minimum use some reg-ex / similar sanity testing to validate the postcodes - such as the links Dmitri gave you above.

If you look at the test cases you have listed - W1ABC and W10ABC are not valid postcodes - if we get that bit correct then the next bit becomes a lot easier.

Extract the Prefix

Assuming you now have a full, valid postcode getting just the first part (outcode) becomes a lot easier - with or without spaces. Because the second half (incode) has a standard format of 9AA, digit-alpha-alpha, I would do it by spotting and removing this, leaving you with just your outcode whether it be W1 From W1 0AA, or W10 from W10 0AA.

Alternatively, if you are using a 3rd party to capture the address - most of them will be able to return the incode and outcode separately for you.

like image 143
Al Mills Avatar answered Dec 13 '22 06:12

Al Mills


The below graphic explains the format of UK postcodes:

Format of UK postcodes

Source: https://www.getthedata.com/postcode (My site) So you can see that you need Outcode which given your requirement (given a full postcode with no space) is simply your space-free postcode minus the last three characters.

In PHP this would be:

$outcode = substr($postcode_no_space, 0, -3)

Of course this does not help with validating the postcode, but as you point out in your comments the question is not about validation.

like image 41
Dan Winchester Avatar answered Dec 13 '22 05:12

Dan Winchester