I need a PHP/Regular Expression to find the parts of a US postal address using php, preferably returned in an array, identified by key values, so i can feed into a Like Search in MySQL. See Examples of the possible input and the output below. This is a challenging problem. Thanx in advance. Please, if you know of a better way to do this by all means let me know. MySQL's boolean search looked interesting for this type of problem. THANKS AGAIN!!
Possible input fields are: Number, Direction, Name, Suffix, City, State, Zip, but direction could come on the either side of number.
Case does not matter, fields may be seperated by commas or spaces
Suffixes need to be validated and abreaiations should be replaced by the full names:
STREET|ST|DRIVE|DR|AVENUE|AVE|ROAD|RD|COURT|CT|CIRCLE|LANE|LN|BOULEVARD|BLVD
Compass directions need to be validated and abreaiations should be replaced by the full names:
W|West|SW|Southwest|NW|Northwest|S|South|E|East|SE|Southeast|NE|Northeast|N|North
Cities and streets may contain more than one word.
Currently the User input is turned into an array
The output should also be an array, where I will then feed to a class that will create the mySql 'AND' logic for the fields.
array(
[number] =>421
[direction] =>'west'
[city] =>'john glenn'
[state] =>'ca'
)
Example of the solution array applied to a query:
WHERE number like '%421%' AND direction like '%west%' AND city like '%john glenn%' AND state like 'ca'
I am currently creating an array with the input and seperating as below:
$addressParts = explode(",", str_replace(" ",",",$PostAddr));
INPUT: las vegas, nv
OUTPUT: city =>las vegas, state => nv
INPUT: 90210
OUTPUT: zip => 90210
INPUT: 910 hamilton 90210
OUTPUT: number => 910, name => hamilton, zip => 90210
INPUT: 910 hamilton ave 90210
OUTPUT: number => 910, name => hamilton, suffix => avenue, zip => 90210
INPUT: 220 hamilton john glenn ca
OUTPUT: number => 220, name => hamilton, city => john glenn, state => ca
INPUT: 421 w 14th st john glenn ca
OUTPUT: number => 421, direction => west, name => 14th, suffix => street, city => john glenn, state => ca
INPUT: 220 hamilton john glenn
OUTPUT: number => 220, name => hamilton, city => john glenn
INPUT: 910 hamilton ave, campbell, ca
OUTPUT: number => 910, name => hamilton, suffix => avenue, city => campbell, state => ca
INPUT: w hamilton ln, john glenn, ca 90210
OUTPUT: direction => west, name => hamilton, suffix => lane, city => john glenn, state => ca, zip => 90210
INPUT: w hamilton ave john glenn ca 90210
OUTPUT: direction => west, name => hamilton, suffix => avenue, city => john glenn, state => ca, zip => 90210
Premium Content
You need an Expert Office subscription to comment.Start Free Trial