-2

I need to extract the first part of postcode for search calculation using a database. Any ideas?! 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 doesnt have second part of the postcode, only the first part is there. Its for UK postcode. So, any ideas?!

Maverick
  • 2,738
  • 24
  • 91
  • 157
  • What do you mean by "some relevant information"? How is this different from your last question [php codeigniter postcode search!](http://stackoverflow.com/q/6316152)? And what are you stuck with exactly? – Pekka Jun 11 '11 at 20:02
  • actually, I am using a database to calculate the nearby postcode and show some information for example stores, in a specified given radius by the user, but the problem with my database is that, the user might enter BS8 1RY, but in the database its only BS8, and its not the same pattern always, the first part can consist 3-4 letter or even 2 letter too. I want to extract that part to use it in my calculation. – Maverick Jun 11 '11 at 20:10
  • Okay, but what *exactly* is your question? – Pekka Jun 11 '11 at 20:30
  • I want to extract that first part of the postcode from the user input, but the problem is, I am not sure whether the first 3 letters form the postcode which I can search in the database or maybe the first 2 or first 4 letters. Again, let me explain why I want that, my database has the first part of post codes, which dont have any specific pattern. The first part in the database has 2,3 or even 4 alphanumeric digits,but the user will enter full post code, which I cant use directly in the calculation. So, for the calculation I need that first part of the postcode extracted. – Maverick Jun 11 '11 at 20:34
  • It depends - a UK postcode fragment (like `W1X`) is a larger area. The smaller the fragment (like `W1`), the larger the area it covers. I still don't understand what your question is? – Pekka Jun 11 '11 at 20:42

1 Answers1

9

This will not cover 100% of all possible UK postcodes, but will do for like 99.999% or so :)

/**
 * @param string $postcode UK Postcode
 * @return string
 */
function getUKPostcodeFirstPart($postcode)
{
    // validate input parameters
    $postcode = strtoupper($postcode);

    // UK mainland / Channel Islands (simplified version, since we do not require to validate it)
    if (preg_match('/^[A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?)\s*?\d[A-Z][A-Z]$/i', $postcode))
        return preg_replace('/^([A-Z]([A-Z]?\d(\d|[A-Z])?|\d[A-Z]?))\s*?(\d[A-Z][A-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

LazyOne
  • 158,824
  • 45
  • 388
  • 391