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
prefixHow 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?
There are 6 possible formats for postcodes in the UK:
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.
The below graphic explains the 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With