Postcode regex help please
Moderator: General Moderators
Postcode regex help please
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
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
No. Postcodes in London can be things like SW1X 1EE.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
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
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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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');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...
..and i just wanna get the post code.
Using your code, the $match array is always empty AND the script never dies
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 3PTUsing your code, the $match array is always empty AND the script never dies
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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...
sorry about that.. didn't notice the "string" example you had earlier.
since you have the postal code in a larger string...
Code: Select all
preg_match('#(?<!ї\w\d])(їA-Z]їA-Z0-9]{2,4})\s+(ї0-9]їA-Z]{2})(?=!ї\w\d])#si', $userPostalCode, $match);
$newPostalCode = trim($matchї1] . ' ' . $matchї2]);
if(empty($newPostalCode)) die('postal code bad');sorry about that.. didn't notice the "string" example you had earlier.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
my bad.. I messed up on the look-ahead:
outputs
Code: Select all
<?php
$userPostalCode = 'North West House, 61/62, Oakhill Trading Estate, Worsley Road North, Walkden, Manchester M28 3PT';
preg_match('#(?<!ї\w\d])(їA-Z]їA-Z0-9]{2,4})\s+(ї0-9]їA-Z]{2})(?!ї\w\d])#si', $userPostalCode, $match);
$newPostalCode = trim($matchї1] . ' ' . $matchї2]);
//if(empty($newPostalCode)) die('postal code bad');
var_export($newPostalCode);
echo "\n\n".strlen($newPostalCode);
?>Code: Select all
'M28 3PT'
7