SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed with postcode data

    Hi,

    I have an application which splits the UK into 3 separate areas based on postcode.

    This is generally based on the incoming bit of the postcode eg. CH1, but occasionally is based on the the incoming bit and the sector eg. CH1 5. Previously I've had 4 separate fields in the form CH 1 5 BR, but the form now uses postcode lookup software and I'm stuck.

    If the postcode is passed as a single string, how can I strip out the first part (which could be CH1 or CH45 or B1) and then the first number from the second part of the postcode?

    I hope this makes sense?
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by pacman2504 View Post
    Hi,

    I have an application which splits the UK into 3 separate areas based on postcode.

    This is generally based on the incoming bit of the postcode eg. CH1, but occasionally is based on the the incoming bit and the sector eg. CH1 5.
    How many actually cross boundaries? Not many.

    Identify those few - and try and snag those first when a user gives you a postcode. eg "CH1"

    In those few cases prompt for these users to enter the single number ( including 0)

    Previously I've had 4 separate fields in the form CH 1 5 BR, but the form now uses postcode lookup software and I'm stuck.
    Are you aware that postcodes like RG14 are legal.

    Google for UK postcode specification

  3. #3
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, yes I am aware of postcode structure.

    But what I need to do is this. Let's assume the postcode in question is CH1 1AA. Previously the form would have posted $postcode1 as "CH", $postcode2 as "1" and $postcode3 as "1". $postcode4 would be "AA" but these two letters are irrelevant for this exercise.

    I therefore had several 'if' statements to allocate the postcode to either area 1, area 2 or area 3.

    Now, the postcode is passed as a single string, so I need a way to extract CH, 1 and 1 as separate variables. Is there a way to do this?
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by pacman2504 View Post
    Now, the postcode is passed as a single string, so I need a way to extract CH, 1 and 1 as separate variables. Is there a way to do this?
    Right, got your point - but is this user input?

    If so then only collect what you want in the first place, as I suggested.

    If not, so its coming from another system - well can you be sure it is a correctly formed and legal postcode?

    If so then its going to be pretty easy:

    PHP Code:
    $pc"CH13 2AA";

    $pc_bits split(' ' $pc );

    $pc_postfix $pc_bits[1] ;

    echo 
    'Prefix = ' substr$pc0);

    echo 
    'PCArea = ' . (int)substr$pc2);

    // you dont want the last letters, and it starts with a number so turn into an int
    echo 'Postfix = ' . (int)$pc_postfix 
    Thats one way of doing it IF you can trust that you are being given a legal postcode.

    Another way is to use a regular expression - if you google for UK Postcode regex then you will find several good ones.

    That regex has to be used inside functions like preg_match, preg_match_all etc.

    I dont know if this is your case but without doubt the best way to go from a security and reliablity POV, is to only allow users to enter letters and numbers in a manner that makes it easy for you to check.

    e.g. none of the above will work for typical errors ppl make, like this:

    CH132AA ( no space )
    CH13-2AA (dash not space )
    CH17 0AA ( illegal pattern )
    CH1OAA (letter O not a zero)

  5. #5
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Cups. The postcode will always be valid so there shouldn't be any issues.

    I think I can see how the code works, but just to confuse matters, the first part of the postcode could be B1, B10, BA1 or BA10 meaning that there could possibly be one or two letters followed by one or two numbers. Does that make it a lot more difficult to split the postcode up?
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Right, yeah, I forgot that ...

    I always try and use native PHP functions where I can, rather than regexes, because I find they are easier to read and faster.

    I think they might help you to understand what is going on too.

    But this might be a job for regexes.

    But, not giving up, I modified a regex found here

    PHP Code:
    $pc"BA3 2AA";

    $reg  "/\b([A-Z]{1,2})([0-9]{1,2}) ([0-9])[ABD-HJLN-UW-Z]{2}/i" ;

    preg_match$reg ,  $pc $matches);

    print_r($matches); 
    So you want $matches[1] $matches[2] $matches[3]

  7. #7
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent. Works a treat. Thanks Cups, much appreciated.
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •