Creating a PHP array of ISO 3166-1 Country Codes
on Thursday 7th January, 2010 Gabe speculated thusly…I recently came across http://opencountrycodes.appspot.com/ which has an ISO 3166-1 list of country codes for various programming languages. I wanted to generate an HTML drop down list for customer to choose from based on these codes. However, there is no PHP version provoided. So taking the XML version found at http://opencountrycodes.appspot.com/xml/ I made a trivial script that would take the codes found in the XML document and organise them in to a PHP array called $XML2PHPCountryCodes and save that array in a file called country_names.php. The output file can then be included in any PHP script making the contained array available.
< ?php
$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);
[...] I just needed a little PHP script. I quickly found one about the country codes and modified it to support [...]
Thanks for this little piece of code
I used it for the 3166-2 country subdivisions codes: http://www.ratdegout.com/moodleitandme/?p=191