SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extract UK postcode from string

    Hi guys

    I need to extract the postcode from the address

    I need to get to this

    Code:
    "$value="Big Ben, Westminster, London, SW1A 0AA, UK";‎
    to this

    Code:
    $value="SW1A 0AA";‎
    Many Thanks

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,265
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    Hi wazo,

    You should be able to do that with a regular expression:
    PHP Code:
    $value "Big Ben, Westminster, London, SW1A 0AA, UK";

    $pattern "/((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))/i";

    preg_match($pattern$value$matches);

    $value matches[0]; // Will give you SW1A 0AA 


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
  •