Reputation: 57
I am learning how to extract data from a string of data to define as variables and use as I'd like. The data source I am parsing is a standard email every time and the address is exactly the same punctuation and format each time. The only difference is that sometimes the zip-code may be the 9-digit code.
123 Riverview Pkwy, Orlando, FL 55556
123 Riverview Pkwy, Orlando, FL 55556-1234
I was able to extract the city like this:
preg_match('/, (.*?)\,/', $xaemailpaste, $insuredcity1);
$insuredcity = $insuredcity1[1];
And that will return "Orlando" perfectly, but I can't get the address, state or zip. Can someone please help me with a regular expression to assign variables to each component?
$streetaddress = 123 Riverview Pkwy
$city = Orlando
$state = FL
$zip = 55556
I have had some success with regex and I'm excited to learn more, but I have spent 2 days trying stuff and looking up things to try and I can't get the expression just right for my exact needs.
Upvotes: 0
Views: 769
Reputation: 27723
This RegEx might help you to divide your addresses into groups, where you can simply extract your listed variables:
([0-9]+)\s([A-Za-z0-9\-\s]+)\,\s([A-Za-z\s]+)\,\s([A-Z]{2})\s([0-9\-]{5,10})
You may work on this RegEx and create additional boundaries, if you wish. It currently has five groups ()
, where groups $2
to $5
are your target variables.
Upvotes: 2