Check if two addresses can be the same in php

I have this address:

Grimshaw Lane, Bollington, Macclesfield SK10 5JB, 

Looking for this address, I get this (from the API):

 Bollington Wharf, Grimshaw Lane, Bollington, United Kingdom 

I know how preg_match works, but I think it should be all the same to compare two similars texts (it seems not the same) and decide whether they are the same address (even if they are slightly different).

+4
source share
3 answers

Obviously, there is no solution that can help you get 100% reliable results, but why not give it a try: send both lines to Google Maps using wget and compare the results. Google has invested at least tens of thousands of man hours in solving the problem you're looking at, why not just let them handle it?

+6
source

I'm not sure if this helps, but I would consider using a combination of using explode to create multiple lines in the levenshtein () array to compare different elements of the array ().

It depends on how many arrays you have to compare, but if you have several (NOT thousands)

The Psudo code would be something like this:

 $search_address = "Bollington Wharf, Grimshaw Lane, Bollington, United Kingdom"; $my_addresses = Array("Grimshaw Lane, Bollington, Macclesfield SK10 5JB", "Different Lane, YabbaDabbaDoo, Otherfield SK12 6BJ", ...); $search_array = explode(',', $search_address); $best_address = array(); $lowest_compare_value = 1000; $lowest_compare_address = array(); foreach($my_addresses as $key => $my_address) { $current_address_array = explode(',', $value); $compare_value = 0; foreach(<elements in $my_address>) { $lowest_value = 1000; foreach(<elements in $search_array) { $new_value = levenshtein($search_element, $my_element); if ($new_value < $lowest_value) { $lowest_value = $new_value; } } $compare_value += $lowest_value; } if($compare_value < $lowest_compare_value) { $lowest_compare_value = $compare_value $lowest_compare_address = $my_address; } } 

Now you should also consider what the maximum plausible levenshtein value might be to check if the address being compared is too remote.

As already mentioned, this method takes time and should NOT be used in an application that requires high speed or you have many local addresses.

+2
source

I created this utility and it has been working for me for a while. Of course, if Google maps changes its API, it must be changed.

 <?php // Queries google maps for the address components function utl_GetAddressComponents( $location ) { $components = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.urlencode($location).'&sensor=false'); $output = json_decode($components); return $output->results[0]; } ?> 

Here is the test file that I used to run from the command line:

 tst_MatchingAddresses.php: <?php require_once( "utl_GetAddressComponents.php" ); $addr1 = $argv[1]; $addr2 = $argv[2]; $gmapsResult1 = utl_GetAddressComponents( $addr1 ); $gmapsResult2 = utl_GetAddressComponents( $addr2 ); $gmapsAddr1 = $gmapsResult1->formatted_address; $gmapsAddr2 = $gmapsResult2->formatted_address; print("Gmap1: ".$gmapsAddr1." ----- argv[1]: ".$argv[1]."\n"); print("Gmap2: ".$gmapsAddr2." ----- argv[2]: ".$argv[2]."\n"); if ( strcmp($gmapsAddr1,$gmapsAddr2) ) { print("==> Addresses match!\n"); } else { print("==> Addresses DO NOT MATCH!\n"); } ?> 

Here is an example command line:

 php tst_MatchingAddresses.php "1600 Pennsylvania Ave, Washington DC" "1600 Pennsylvania Avenue, 20500" 

Output Example:

 Gmap1: 1600 Pennsylvania Avenue Northwest, President Park, Washington, DC 20500, USA ----- argv[1]: 1600 Pennsylvania Ave, Washington DC Gmap2: 1600 Pennsylvania Avenue Northwest, President Park, Washington, DC 20500, USA ----- argv[2]: 1600 Pennsylvania Avenue, 20500 ==> Addresses match! 

Note. You can enter an argument into get_contents_file, replace urlencode, etc. address directly in the browser and display json results.

+1
source

All Articles