sfullman
05-08-2008, 10:04 PM
here is some code that was handy for me to find the city, state, and zip code + 4 - also will find Canadian zip codes if you wish to mail to canadians :) just kidding
function parse_citystatezip($address){
/***
2008-05-08 used first for quickbooks import => database table
johnson city, tex|tx|texas 78691[-1234]
vancouver, bc A0A 0A0
charlotte, n.c. 02899
***/
$address=trim($address);
$zip='([a-z0-9][0-9][a-z0-9][- ]*[0-9][a-z0-9][0-9]*)([- ]+([0-9]{4}))*';
$state='([.a-z]{2,4})';
if(!preg_match("/\s+$state\s+$zip".'$'."/i",$address,$a))return false;
$state=strtoupper(str_replace('.','',$a[1]));
$zip=$a[2];
if($plus4=$a[4])$zipplus4=$zip.'-'.$plus4;
$city=substr($address,0,strlen($address)-strlen($a[0]));
$city=rtrim($city,',');
$order=array(
'city',
'state',
'zip',
'plus4',
'zipplus4'
);
foreach($order as $v){
echo $v . '<br>
';
isset($$v)?$b[$v]=$$v:'';
}
return $b;
}
echo '<pre>';
print_r(parse_citystatezip($addr));
function parse_citystatezip($address){
/***
2008-05-08 used first for quickbooks import => database table
johnson city, tex|tx|texas 78691[-1234]
vancouver, bc A0A 0A0
charlotte, n.c. 02899
***/
$address=trim($address);
$zip='([a-z0-9][0-9][a-z0-9][- ]*[0-9][a-z0-9][0-9]*)([- ]+([0-9]{4}))*';
$state='([.a-z]{2,4})';
if(!preg_match("/\s+$state\s+$zip".'$'."/i",$address,$a))return false;
$state=strtoupper(str_replace('.','',$a[1]));
$zip=$a[2];
if($plus4=$a[4])$zipplus4=$zip.'-'.$plus4;
$city=substr($address,0,strlen($address)-strlen($a[0]));
$city=rtrim($city,',');
$order=array(
'city',
'state',
'zip',
'plus4',
'zipplus4'
);
foreach($order as $v){
echo $v . '<br>
';
isset($$v)?$b[$v]=$$v:'';
}
return $b;
}
echo '<pre>';
print_r(parse_citystatezip($addr));