1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
function parse_address ($in) {
if(stripos($in,'box') !== false) {
$matches = array();
if(preg_match('/^P([. ])?O([. ])?BOX( )?([0-9]+)$/i',$in,$matches)) {
$out['housenum'] = $matches[4];
$switch = true;
} elseif(preg_match('/^BOX( )?([0-9]+)$/i',$in,$matches)) {
$out['housenum'] = $matches[2];
$switch = true;
} else {
$switch = false;
}
if($switch) {
$out['streetname'] = 'PO Box';
$out['streettype'] = '';
$out['post_direction'] = '';
$out['streetdir'] = '';
$out['apt'] = '';
return $out;
}
}
$in = strtoupper($in);
$in = str_replace('.','',$in);
$suffixes = array(
'ALY','ANX','ARC','AVE','BYU','BCH','BND','BLF','BLFS','BTM','BLVD','BR','BRG','BRK','BRKS','BG','BGS',
'BYP','CP','CYN','CPE','CSWY','CTR','CTRS','CIR','CIRS','CLF','CLFS','CLB','CMN','COR','CORS','CRSE','CT',
'CTS','CV','CVS','CRK','CRES','CRST','XING','XRD','CURV','DL','DM','DV','DR','DRS','EST','ESTS','EXPY','EXT',
'EXTS','FALL','FLS','FRY','FLD','FLDS','FLT','FLTS','FRD','FRDS','FRST','FRG','FRGS','FRK','FRKS','FT','FWY',
'GDN','GDNS','GTWY','GLN','GLNS','GRN','GRNS','GRV','GRVS','HBR','HBRS','HVN','HTS','HWY','HL','HLS','HOLW',
'INLT','IS','ISS','ISLE','JCT','JCTS','KY','KYS','KNL','KNLS','LK','LKS','LAND','LNDG','LN','LGT','LGTS',
'LF','LCK','LCKS','LDG','LOOP','MALL','MNR','MNRS','MDW','MDWS','MEWS','ML','MLS','MSN','MTWY','MT','MTN',
'MTNS','NCK','ORCH','OVAL','OPAS','PARK','PKWY','PASS','PSGE','PATH','PIKE','PNE','PNES','PL','PLN','PLNS',
'PLZ','PT','PTS','PRT','PRTS','PR','RADL','RAMP','RNCH','RPD','RPDS','RST','RDG','RDGS','RIV','RD','RDS',
'RTE','ROW','RUE','RUN','SHL','SHLS','SHR','SHRS','SKWY','SPG','SPGS','SPUR','SQ','SQS','STA','STRA','STRM',
'ST','STS','SMT','TER','TRWY','TRCE','TRAK','TRFY','TRL','TUNL','TPKE','UPAS','UN','UNS','VLY','VLYS','VIA',
'VW','VWS','VLG','VLGS','VL','VIS','WALK','WALL','WAY','WAYS','WL','WLS','STREET','ROAD','COURT','AV');
$directions = array('N','S','E','W','NW','NE','SW','SE');
$out = array();
$address_parts = explode(' ',$in);
$out['housenum'] = array_shift($address_parts);
if(in_array($address_parts[0],$directions)) {
$out['streetdir'] = array_shift($address_parts);
} else {
$out['streetdir'] = '';
}
$temp_addy = implode(' ',$address_parts);
if(strpos($temp_addy,'APT #')) {
$amatches = array();
preg_match('/APT #([^ ]+)/',$temp_addy,$amatches);
$temp_addy = preg_replace('/APT #[^ ]+/','',$temp_addy);
$out['apt'] = $amatches[1];
} elseif(strpos($temp_addy,'APT ')) {
$amatches = array();
preg_match('/APT ([^ ]+)/',$temp_addy,$amatches);
$temp_addy = preg_replace('/APT [^ ]+/','',$temp_addy);
$out['apt'] = $amatches[1];
} elseif(strpos($temp_addy,'# ')) {
$amatches = array();
preg_match('/# ([^ ]+)/',$temp_addy,$amatches);
$temp_addy = preg_replace('/# [^ ]+/','',$temp_addy);
$out['apt'] = $amatches[1];
} elseif(strpos($temp_addy,'#')) {
$amatches = array();
preg_match('/#([^ ]+)/',$temp_addy,$amatches);
$temp_addy = preg_replace('/#[^ ]+/','',$temp_addy);
$out['apt'] = $amatches[1];
} else {
$out['apt'] = '';
}
$temp_addy = rtrim($temp_addy);
$address_parts = explode(' ',$temp_addy);
$post_direction = array_pop($address_parts); // Pull out street type
if(!in_array($post_direction,$directions)) { // Stick it back in if it's not a street type
$address_parts[] = $post_direction;
$post_direction = '';
}
$out['post_direction'] = $post_direction;
$street_type = array_pop($address_parts); // Pull out street type
if(!in_array($street_type,$suffixes)) { // Stick it back in if it's not a street type
$address_parts[] = $street_type;
$street_type = '';
}
$out['streettype'] = $street_type;
$out['streetname'] = implode(' ',$address_parts);
return $out;
}
|