• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 598
  • Last Modified:

Split UK postcode

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
abenbow
Asked:
abenbow
  • 2
1 Solution
 
TeRReFCommented:
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
 
abenbowAuthor Commented:
lovely
0
 
TeRReFCommented:
You're welcome :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now