Solved

Split UK postcode

Posted on 2007-03-29
3
582 Views
Last Modified: 2010-08-05
I have a UK postcode in the following format

DA23 7YT

I need to split it and return 2 parts

1 - DA
2- DA23

note the postcode COULD be

B2 7OP

so I would need

1 - B
2 - B2

OR

L23 8NM

so I would need

1 - L
2 - L23


tia
0
Comment
Question by:abenbow
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 18820522
How about:

<?php

function splitPostCode($s) {
        if (preg_match('/^([a-z]+)(\d+)/i', $s, $m) === 1)
                return array($m[1], $m[1].$m[2]);

        return false;
}

$s = 'DA23 7YT';
print_r(splitPostCode($s));

?>
0
 

Author Comment

by:abenbow
ID: 18820581
lovely
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 18820583
You're welcome :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now