Matching a UK Postcode

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
construct
Forum Newbie
Posts: 5
Joined: Sat Jul 02, 2011 12:58 pm

Matching a UK Postcode

Post by construct »

Useful regex to match a valid UK Postcode.

Code: Select all

$postal = 'AL42PT';
 
if(preg_match("/^[A-Z]{1,2}([0-9]{1,2}|[0-9]{1,1}[A-Z]{1,1})( |)[0-9]{1,1}[A-Z]{2,2}$/", $postal)) {
 
  echo $postal;
 
}
Source: TutorialCadet
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Matching a UK Postcode

Post by McInfo »

Post Reply