Re321 Posted April 13, 2010 Share Posted April 13, 2010 An example of the address is: 494 Main, Brook, MA, 19076. I've tried many regular expressions but I just can't do it, The street name can be any length but the state abbreviation can only be 2 or 3 characters long, The zip code is 5 ({5}?) I've wrote the first part, something like ((\d+)\s+(\w+)),\s+(\w+) .. But I don't know what to do for the rest, I really need help! Link to comment https://forums.phpfreaks.com/topic/198449-how-do-i-capture-data-from-a-long-address-complex/ Share on other sites More sharing options...
oni-kun Posted April 13, 2010 Share Posted April 13, 2010 The rest isn't too hard, Here: "/((\d+)\s+(\w+)),\s+(\w+),\s+([A-Z]{2,3}),\s+(\d{5})/" Note the ranges, so it will conform to your length rules. Link to comment https://forums.phpfreaks.com/topic/198449-how-do-i-capture-data-from-a-long-address-complex/#findComment-1041326 Share on other sites More sharing options...
Re321 Posted April 13, 2010 Author Share Posted April 13, 2010 The rest isn't too hard, Here: "/((\d+)\s+(\w+)),\s+(\w+),\s+([A-Z]{2,3}),\s+(\d{5})/" Note the ranges, so it will conform to your length rules. Thanks oni for your fast reply. How would I use this to echo all matches? I simplty need it on one address, I may loop it through addresses later but I need to list the captured data if it's right! Link to comment https://forums.phpfreaks.com/topic/198449-how-do-i-capture-data-from-a-long-address-complex/#findComment-1041328 Share on other sites More sharing options...
oni-kun Posted April 13, 2010 Share Posted April 13, 2010 It's simple, Matches are stored in an array; so you could do this. $address = "494 Main, Brook, NC, 19076"; $valid = "/((\d+)\s+(\w+)),\s+(\w+),\s+([A-Z]{2,5}),\s+(\d{5})/"; if ( preg_match( $valid, $address, $matches ) == 1 ) { print "Street: ${matches[1]}<br />"; print "Street number: ${matches[2]}<br />"; print "Street name: ${matches[3]}<br />"; print "City: ${matches[4]}<br />"; print "State: ${matches[5]}<br />"; print "Zip: ${matches[6]}<br />"; } Link to comment https://forums.phpfreaks.com/topic/198449-how-do-i-capture-data-from-a-long-address-complex/#findComment-1041331 Share on other sites More sharing options...
Re321 Posted April 13, 2010 Author Share Posted April 13, 2010 Thanks oni-kun a whole lot, I've been racking my mind for an hour on this one. :-\ I like this forum alot Link to comment https://forums.phpfreaks.com/topic/198449-how-do-i-capture-data-from-a-long-address-complex/#findComment-1041340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.