The Facebook API provides the user with the current location in two formats: name and identifier. The ID can be something like 104022926303756. The name can be something like Palo Alto, California or Beijing, China. From these two fields, how can I extract a country? All US locations are in the form of [City], [State] , while all non-US locations are in the form of [City], [Country] . Can I encode something less hacky than:
$states = array( 'Alabama' 'Alaska', 'Arizona', // ... ); $country = 'USA'; if (!in_array($locationName, $states)) { preg_match('#, ([az]+$)#i', $locationName, $match); $country = $match[1]; }
source share