Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.
Moderator: General Moderators
jason
Site Admin
Posts: 1767 Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:
Post
by jason » Mon Oct 20, 2003 10:50 am
Using the preg_* functions:
USA
Code: Select all
'/^\d{5}(-\d{4})?$/'
Canadian
Code: Select all
'/^їa-z]\dїa-z] ?\dїa-z]\d$/i'
Example:
Code: Select all
<?php
if ( preg_match('/^\d{5}(-\d{4})?$/', '55082') ) {
echo 'yes';
} else {
echo 'no';
}
if ( preg_match('/^[a-z]\d[a-z] ?\d[a-z]\d$/i', 'H1S 1T6') ) {
echo 'yes';
} else {
echo 'no';
}
?>
If you have Pregular Expressions for other countries, list those as here, as well.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Mon Oct 20, 2003 2:42 pm
german zip codes simply consist of five digits
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Oct 21, 2003 2:46 am
British, will match BA12 3PR type post codes, as well as inner London type ones (EC1M 3AA) and the space is optional:
Code: Select all
/^їa-z]{1,2}\d{1,2}їa-z]? ?\dїa-z]{2}$/i
Mac
AVATAr
Forum Regular
Posts: 524 Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:
Post
by AVATAr » Tue Oct 21, 2003 5:50 pm
Uruguayan zip codes, the same as german, five digits
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Sat Oct 25, 2003 4:02 am
Swedish, 5 digits, optional space after the first 3.
Code: Select all
/^\d{3}? ?\d{2}$/
n00b Saibot
DevNet Resident
Posts: 1452 Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:
Post
by n00b Saibot » Fri Dec 24, 2004 7:03 am
India ones are simple 6 digits long.