-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreet_address.php
127 lines (113 loc) · 3.32 KB
/
street_address.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
class StreetAddressHelper extends Helper {
/*
Returns the array of each element of a street address, into the categories:
property_address (or house number)
direction (abbreviated; n s e w)
unit (apt number)
suffix (also abbreviated, st, blvd, etc.)
Note: this does not account for City, State, and ZIP!!!
Why? Cuz I didn't need it for my project.
Add the functionality if you need it--pay it forward, man!
*/
//reference arrays
var $directions = array(
'N1'=>'North',
'S1'=>'South',
'E1'=>'East',
'W1'=>'West',
'N2'=>'N.',
'S2'=>'S.',
'E2'=>'E.',
'W2'=>'W.',
'N'=>'N',
'S'=>'S',
'E'=>'E',
'W'=>'W'
);
var $suffixes = array(
'Ave' => 'Avenue',
'St' => 'Street',
'Ct' => 'Court',
'Cir' => 'Circle',
'Blvd' => 'Boulevard',
'Dr' => 'Drive',
'Pl' => 'Place',
'Cr' => 'Creek',
'Land' => 'Land',
'Rd' => 'Road',
'Ln' => 'Llane',
'Tr' => 'Trail',
'Pkwy'=>'Parkway'
);
var $suffixes_abbr = array(
'Circle' => 'Cir',
'Place' => 'Pl',
'Court' => 'Ct',
'Street' => 'St',
'Avenue' => 'Ave',
'Road' => 'Rd',
'Way' => 'Way',
'Boulevard' => 'Blvd',
'Drive' => 'Dr',
'Lane'=>'Ln',
'Trail'=>'Trl',
'Parkway'=>'Pkwy',
'Terrace'=>'Ter'
);
/**
* returns an array of street address components
*
* @param unknown_type $address
* @param unknown_type $type
*/
function parse($address=null, $type=1){
switch($type){
case 1: // 1 = address type (i.e. house number direction street unit)
//break down into array
$addrArray=(explode(' ',$this->sentence_case($address)));
//first element virtually always house number
$record['property_address']=array_shift($addrArray);
//sanitize unit number
if(in_array('Unit',$addrArray)){
$record['unit']=str_replace('#','',trim(array_pop($addrArray)));
array_pop($addrArray);
}elseif(in_array('Apt',$addrArray)){
$record['unit']=str_replace('#','',trim(array_pop($addrArray)));
array_pop($addrArray);
}elseif(in_array('#',$addrArray)){
$record['unit']=trim(array_pop($addrArray));
array_pop($addrArray);
}
$unit = trim(array_pop($addrArray));
if(is_numeric($unit)){
$record['unit']=$unit;
}elseif(strpos($unit,'#') !== false){
$record['unit']=str_replace('#','',$unit);
}else{
$addrArray[] = $unit;
}
//sanitize and apply direction
$direction = array_intersect($this->directions,$addrArray);
if(!empty($direction)){
unset($addrArray[key(array_intersect($addrArray,$directions))]);
$record['direction']=strtoupper(substr(key($direction),0,1));
}
//... street suffix
$suffix = array_intersect($this->suffixes,$addrArray);
if(!empty($suffix)){
unset($addrArray[key(array_intersect($addrArray,$this->suffixes))]);
$record['suffix']=key($suffix);
}
$suffix = array_intersect($this->suffixes_abbr,$addrArray);
if(!empty($suffix)){
unset($addrArray[key(array_intersect($addrArray,$this->suffixes_abbr))]);
$record['suffix']=array_shift($suffix);
}
$record['street']=implode(' ',$addrArray);
return $record;
break;
}
}
}
?>