Solved

Split UK postcode

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP & MySQL - Rounding Results from a Select Query 3 26
php function to remove a file 31 43
php convert date format 3 23
ignore other .htaccess 2 45
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 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