Postcode regex help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Postcode regex help please

Post by JayBird »

Say i have a string stored ina variable called $address.

I wanna pull the postcode out of that string.

"Somestreet, somewhere, someplace, rt24 4hx."

how do i get just the postcode, bearing in mind postcode can vary in length

YO6 4GJ
YO61 3HX

and there may not always be a fullt stop at the end.

I think i am right in saying that UK postacode are always

-2 letter
-1 or 2 numbers
-optional space
-1 number
-2 letters

i really am rubbish with regex stuff, can anyone help?

Thanks

Mark
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Postcode regex help please

Post by onion2k »

Bech100 wrote:I think i am right in saying that UK postacode are always

-2 letter
-1 or 2 numbers
-optional space
-1 number
-2 letters
No. Postcodes in London can be things like SW1X 1EE.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if someone will post an example list of many differing postal codes, I'll write the regex.. I just want to make sure it's as close to the format as possible. :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

I found out this information froma javascript postcode validator, this would do for my purposes

Postcodes must conform to the following rules:-

The total length must be 6,7, or 8 characters, a gap (space character) must be included
The inward code, the part to the right of the gap, must always be 3 characters
The first character of the inward code must be numeric
The second and third characters of the inward code must be alpha
The outward code, the part to the left of the gap, can be 2,3, or 4 characters
The first character of the outward code must be alpha
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

preg_match('#^\s*(їA-Z]їA-Z0-9]{2,4})\s+(ї0-9]їA-Z]{2})\s*$#si', $userPostalCode, $match);
$newPostalCode = $matchї1] . ' ' . $matchї2];
if(strlen($newPostalCode) < 6) die('postal code bad');
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Thanks for that feyd, but it doesn't seem to be working.

i am pulling this info from the DB.

An example of the info is...

Code: Select all

North West House, 61/62, Oakhill Trading Estate, Worsley Road North, Walkden, Manchester M28 3PT
..and i just wanna get the post code.

Using your code, the $match array is always empty AND the script never dies
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

just noticed you changed the code, but i still dont get a match returned
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I tweaked the original probably after you copied it.. I realized it wouldn't die because there would be at least the space in it..

since you have the postal code in a larger string...

Code: Select all

preg_match('#(?<!&#1111;\w\d])(&#1111;A-Z]&#1111;A-Z0-9]&#123;2,4&#125;)\s+(&#1111;0-9]&#1111;A-Z]&#123;2&#125;)(?=!&#1111;\w\d])#si', $userPostalCode, $match);
$newPostalCode = trim($match&#1111;1] . ' ' . $match&#1111;2]);
if(empty($newPostalCode)) die('postal code bad');

sorry about that.. didn't notice the "string" example you had earlier.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

hmmm...still not returning any matches for the string example above and any others i pass to it!?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

my bad.. I messed up on the look-ahead:

Code: Select all

<?php

	$userPostalCode = 'North West House, 61/62, Oakhill Trading Estate, Worsley Road North, Walkden, Manchester M28 3PT';

	preg_match('#(?<!&#1111;\w\d])(&#1111;A-Z]&#1111;A-Z0-9]&#123;2,4&#125;)\s+(&#1111;0-9]&#1111;A-Z]&#123;2&#125;)(?!&#1111;\w\d])#si', $userPostalCode, $match);
	
	$newPostalCode = trim($match&#1111;1] . ' ' . $match&#1111;2]);
	//if(empty($newPostalCode)) die('postal code bad');
	
	var_export($newPostalCode);
	echo "\n\n".strlen($newPostalCode);

?>
outputs

Code: Select all

'M28 3PT'

7
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Mate, that work perfectly...your a star!!! :D
Post Reply