Hi guys,
I guess i can help here...
All it takes is somone who under stands UK POST codes. :-) (I have the full UK table in MySQL with approx 7,000,000 rows)
First a little theory.
The First Digit Grouping is called the Out Code. This part of the code identifies the UK sorting office responsible for mail to that address, and the district in that area.
The second half is the Incode and defines the exact location in the post area where that address resides.
The Outcode for many years was only 3 chars, until central & greater london exploded with so many new districts that there was not enough digits in the outcode to accomodate the expansions.
This then lead to most of the big citys segregating the post codes up into sub districts, and we now find that most of the big citys now have 4 charachters.
If your not to bothered about doing an actual preg to check the valididty of the first half, then the way i usually split them is as follows:
$postcode = "DH1 2BC";
$temp = explode(" ",$postcode);
$outcode = trim($temp[0]);
$incode = trim($temp[1]);
of course the problem you now have is if the post code has been entered without a space in it, in which case you then need to figure out where the split should be.
The good thing is that the outcode is always formatted as letters then digits, and is never more than 2 letters, and never more than 2 digits and the incode is always formatted as 1 number and 2 letters.
This means that in theory the following regular expression should always get the in code.
/.*(\d[A-Za-z]{2})/
Now asuming that all you have in the string is your post code, then the following should work with or without the seperating space:
if(preg_match("/(.*)(\d[A-Za-z]{2})/",$postcode,$matches))
{
$outcode = trim($matches[1]);
$incode = trim($matches[2]);
}
If you still have problems with this, then let me know and i'll dig through my code library see what i have that helps.
Cheers
Shawty