How to convert a country string to its ISO 3166-1 code using Zend?

Does anyone know if Zend_Locale can be used to get the ISO 3166-1 code for a country if you have the name of the country as a string? For example, I have "Nederland", so I would like to get "NL".

+4
source share
4 answers

You should look at Zend_Locale :: getTranslation (). You can get a list (simple array) of country names, and then you can use array_search () to get the necessary "country key"!

+2
source

I do not know if Zend has something for this, but it is quite easy to do it yourself.

This guide shows how you can get the latest list of ISO 3166-1 country codes in XML format, parse it and then create a PHP file that can be included when you need an array of country code conversions:

$str = file_get_contents('http://opencountrycodes.appspot.com/xml/'); $xml = new SimpleXMLElement($str); $out = '$countries'." = array(\n"; foreach ($xml->country as $country) { $out .= "'{$country['code']}' => \"{$country['name']}\",\n"; } $out .= ");"; file_put_contents('country_names.php', $out); 

Alternatively, you can save it as a CSV file and load it using the PHP fgetcsv() function . This will probably be preferable to IMO. Or, to hell, you can just save the XML and parse it when you load it.

+3
source

I'm not sure how calls can be made in Zend HTTp, but here is probably a good resource for the link.

Use Yahoo! Location data that maps freeform strings to WOE identifiers. For countries, WOE identifiers are ISO 3166-1 codes.

To convert a freeform string to a WOE identifier, you can use the GeoPlanet APIs: http://developer.yahoo.com/geo/geoplanet/

+3
source

I made an updated collection of country names and ISO 3166 country codes at:

https://github.com/johannesl/Internationalization

You can use it to convert the code name => country and reverse.

I also create a collection of common country aliases that will appear on github.

+2
source

Source: https://habr.com/ru/post/1314835/


All Articles