Solved

Split UK postcode

Posted on 2007-03-29
3
580 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to dynamically set the form action using jQuery.

706 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