0

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.

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    Why not just `explode()` the string by comma and then extract the zip code from the last piece. *Much* easier to do. – John Conde Apr 20 '19 at 02:23
  • Possible duplicate of [How to parse freeform street/postal address out of text, and into components](https://stackoverflow.com/questions/11160192/how-to-parse-freeform-street-postal-address-out-of-text-and-into-components) – Ildar Akhmetov Apr 20 '19 at 06:47

1 Answers1

2

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})

RegEx

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.

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    I'd suggest `(\d{5}(?:-\d{4})?)` for the ZIP+4; the way you've got it there `--------` is valid. – miken32 Apr 20 '19 at 03:43