Page 1 of 1

Postcode regex help please

Posted: Mon Jan 24, 2005 6:30 am
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

Re: Postcode regex help please

Posted: Mon Jan 24, 2005 7:03 am
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.

Posted: Mon Jan 24, 2005 8:19 am
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. :)

Posted: Mon Jan 24, 2005 8:33 am
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

Posted: Mon Jan 24, 2005 8:40 am
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');

Posted: Mon Jan 24, 2005 8:57 am
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

Posted: Mon Jan 24, 2005 8:59 am
by JayBird
just noticed you changed the code, but i still dont get a match returned

Posted: Mon Jan 24, 2005 9:01 am
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.

Posted: Mon Jan 24, 2005 9:11 am
by JayBird
hmmm...still not returning any matches for the string example above and any others i pass to it!?

Posted: Mon Jan 24, 2005 9:26 am
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

Posted: Mon Jan 24, 2005 9:45 am
by JayBird
Mate, that work perfectly...your a star!!! :D