1

Background: I am attempting to run some addresses through an API to get GPS coordinates for routing purposes. The front end interface is simple. instead of dragging you through a description, here's a screen capture of it

Front end interface

easy-peasy, right? When the end user plops down his list of addresses and clicks submit, I explode the list into an array and replace all the spaces with %20 (the lookup API needs this format). Problem is that sometimes the lookup api doesn't like the city and kicks back a null set of data. easiest way to overcome this is to not send the city data in the first place since a ZIP code will suffice. how would I go about stripping it out? am I overthinking this? would it be more acceptable to ask the user not to put it in?

for reference, here is a dump oof an array after submission of data:

array(10) { 
[0]=> string(50) "3464%20Edgewood%20Ave.%20Fort%20Myers%20FL%2033916" 
[1]=> string(50) "2401%20Euclid%20Avenue%20Fort%20Myers%20FL%2033901" 
[2]=> string(55) "2751%20Oak%20Street%20Fort%20Myers%20Beach%20FL%2033931" 
[3]=> string(51) "2323%20Ford%20Street%20Fort%20Myers%20FL%2033916%20" 
[4]=> string(56) "1200%20Homestead%20Rd%20N.%20Lehigh%20Acres%20FL%2033936" 
[5]=> string(36) "13280%20Griffin%20Drive%20FL%2033913" 
[6]=> string(52) "3400%20SW%2017th%20Place%20Cape%20Coral%20FL%2033914" 
[7]=> string(58) "1601%20Skyline%20Drive%20North%20Fort%20Myers%20FL%2033903" 
[8]=> string(56) "1800%20Unice%20Avenue%20N.%20Lehigh%20Acres%20FL%2033971" 
[9]=> string(50) "3464%20Edgewood%20Ave.%20Fort%20Myers%20FL%2033916" 
}

I've tried a str_replace() as well as explodeing the array to a multidimensional array with limited results as street and city names do not have a standard length, I'm thinking of trying a regex next, but I feel like it will be near impossible to cover all situations.

Zak
  • 6,976
  • 2
  • 26
  • 48
  • Instead of trying to manually encode the array, you should use the built in HTTP query builder: https://www.php.net/manual/en/function.http-build-query.php – Derek Pollard Sep 16 '19 at 21:44
  • You could match zip code quite easy with a regex: `/[A-Z]{2}\s+?[0-9]{5,7}/g` – Chris Hemmens Sep 16 '19 at 21:48
  • where is the zip code located in your string? at the begining? at the end? – Marius Danciu Sep 16 '19 at 21:54
  • If it is at the end trim() the possibles spaces and then substr("string", -5) should do. – Marius Danciu Sep 16 '19 at 22:00
  • 1
    @derek I'm diggin the approach you mentioned as well as the regex approach from chris. Only issue with the regex is that I'll still need the rest of the address. – Justin Egan Sep 16 '19 at 22:37
  • @chris. I'm looking into the regex approach you mentioned. it would work if all I needed was the zip code. I also need the address (everything but the city). – Justin Egan Sep 16 '19 at 22:40
  • @marius I see where you're going. I thought of this first, but it ended up getting shot down as it also needs to work for canadian postal codes (6 digits) and 10 digit zip codes. – Justin Egan Sep 16 '19 at 22:42
  • @JustinEgan Zipcode + house number would be possible with a regex. Im not sure about city names, that's getting very complicated very fast. You would have to keep a list of city names somewhere. Then the easiest thing to do is remove them from the string. Otherwise you could look into more complex solutions like machine learning. https://towardsdatascience.com/addressnet-how-to-build-a-robust-street-address-parser-using-a-recurrent-neural-network-518d97b9aebd – Chris Hemmens Sep 16 '19 at 22:47
  • @marius that's what I was thinking. I could keep a database of city names and compare against it, but i feel like responses would take an impractical time to come back to the end user. instead, im considering a redesign of the form which will force the end user to separate the city from the zip code. – Justin Egan Sep 16 '19 at 22:54
  • I would be very careful of using regex to parse street addresses. There are known issues that are explained [here](https://smartystreets.com/articles/regular-expressions-for-street-addresses) – camiblanch Sep 18 '19 at 01:49
  • I ultimately decided not to opting instead to force the user to enter data in a certain way. It was super easy/barely an inconvenience – Justin Egan Sep 18 '19 at 19:23

1 Answers1

0
<?php

$string = "
3464 Edgewood Ave. Fort Myers FL 33916
2401 Euclid Avenue Fort Myers FL 33901
2751 Oak Street Fort Myers Beach FL 33931
2323 Ford Street Fort Myers FL 33916 
1200 Homestead Rd N. Lehigh Acres FL 33936
13280 Griffin Drive FL 33913
3400 SW 17th Place Cape Coral FL 33914
1601 Skyline Drive North Fort Myers FL 33903
1800 Unice Avenue N. Lehigh Acres FL 33971
3464 Edgewood Ave. Fort Myers FL 33916";

$out = [];

// match zip code
preg_match_all("/[A-Z]{2}\s+?[0-9]{5}/",$string,$out);
print_r($out);

output:

Array
(
    [0] => Array
        (
            [0] => FL 33916
            [1] => FL 33901
            [2] => FL 33931
            [3] => FL 33916
            [4] => FL 33936
            [5] => FL 33913
            [6] => FL 33914
            [7] => FL 33903
            [8] => FL 33971
            [9] => FL 33916
        )

)
Chris Hemmens
  • 367
  • 2
  • 11