Php get the first part of postcode?

S*?(\dA-ZA-Z)$/i', '$1', $postcode); // British Forces if (preg_match('/^(BFPO)\s*?(\d{1,4})$/i', $postcode)) return preg_replace('/^(BFPO)\s*?(\d{1,4})$/i', '$1', $postcode); // overseas territories if (preg_match('/^(ASCN|BBND|BIQQ|FIQQ|PCRN|SIQQ|STHL|TDCU|TKCA)\s*?(1ZZ)$/i', $postcode)) return preg_replace('/^(A-Z{4})\s*?(1ZZ)$/i', '$1', $postcode); // well ... even other form of postcode... return it as is return $postcode; } Test data: echo 'TW135BQ -> ', getUKPostcodeFirstPart('TW135BQ'), "\n"; echo 'gir0aa -> ', getUKPostcodeFirstPart('gir0aa'), "\n"; echo 'RG5 3SQ -> ', getUKPostcodeFirstPart('RG5 3SQ'), "\n"; echo 'AB51 0GQ -> ', getUKPostcodeFirstPart('AB51 0GQ'), "\n"; echo 'YO104EA -> ', getUKPostcodeFirstPart('YO104EA'), "\n"; echo 'SE154NS -> ', getUKPostcodeFirstPart('SE154NS'), "\n"; echo 'W1B4BD -> ', getUKPostcodeFirstPart('W1B4BD'), "\n Results: TW135BQ -> TW13 gir0aa -> GIR0AA RG5 3SQ -> RG5 AB51 0GQ -> AB51 YO104EA -> YO10 SE154NS -> SE15 W1B4BD -> W1B Please note: It is your responsibility to provide valid postcode EDIT: oops, it does not cover GIR 0AA, sorry.

S*?(\dA-ZA-Z)$/i', '$1', $postcode); // British Forces if (preg_match('/^(BFPO)\s*?(\d{1,4})$/i', $postcode)) return preg_replace('/^(BFPO)\s*?(\d{1,4})$/i', '$1', $postcode); // overseas territories if (preg_match('/^(ASCN|BBND|BIQQ|FIQQ|PCRN|SIQQ|STHL|TDCU|TKCA)\s*?(1ZZ)$/i', $postcode)) return preg_replace('/^(A-Z{4})\s*?(1ZZ)$/i', '$1', $postcode); // well ... even other form of postcode... return it as is return $postcode; } Test data: echo 'TW135BQ -> ', getUKPostcodeFirstPart('TW135BQ'), "\n"; echo 'gir0aa -> ', getUKPostcodeFirstPart('gir0aa'), "\n"; echo 'RG5 3SQ -> ', getUKPostcodeFirstPart('RG5 3SQ'), "\n"; echo 'AB51 0GQ -> ', getUKPostcodeFirstPart('AB51 0GQ'), "\n"; echo 'YO104EA -> ', getUKPostcodeFirstPart('YO104EA'), "\n"; echo 'SE154NS -> ', getUKPostcodeFirstPart('SE154NS'), "\n"; echo 'W1B4BD -> ', getUKPostcodeFirstPart('W1B4BD'), "\n"; Results: TW135BQ -> TW13 gir0aa -> GIR0AA RG5 3SQ -> RG5 AB51 0GQ -> AB51 YO104EA -> YO10 SE154NS -> SE15 W1B4BD -> W1B Please note: It is your responsibility to provide valid postcode. EDIT: oops, it does not cover GIR 0AA, sorry.

I havent checked it, but I am pretty sure it will work. Thanks a lot. :) – mad_programmer Jun 11 at 20:52.

I need to extract the first part of postcode for search calculation using a database. I need this to implement a search module in the site, which looks out for some relevant information according to the post code. But the database which I am having for post code, it doesn't have second part of the postcode, only the first part is there.

Its for UK postcode. So, any ideas?!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions