UK Postcode

I have a list of UK Postcode that I need to check for the correct format.  

Users address are entered like: $address = "street, town, city, county, postcode";

I split these up into parts like so: $part = explode(',',$Address); which gives me:

$part[0] = 'street';
$part[1] = 'town';
$part[2] = 'city';
$part[3] = 'county';
$part[4] = 'postcode';

But some users enter their phone number instead of the postcode which gives me a pain in the head...

So i'd like to check the last part in the string so I can either put the value in a $telephone = '0123456789'; or $part[4] = 'AB10 1LB';

So how would i check the format of a Postcode? example: AB10 1LB

thank you
LVL 16
ellandrdAsked:
Who is Participating?
 
RoonaanConnect With a Mentor Commented:
Hi,

if(preg_match('/^[a-z]+\d+\s+\d+[a-z]+$/i', $part[4])) {
  //postcode
} else {
  //tel
}

-r-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.