Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Split UK postcode

Posted on 2007-03-29
Medium Priority
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


L23 8NM

so I would need

1 - L
2 - L23

Question by:abenbow
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 29

Accepted Solution

TeRReF earned 2000 total points
ID: 18820522
How about:


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';


Author Comment

ID: 18820581
LVL 29

Expert Comment

ID: 18820583
You're welcome :)

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

636 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