Create a PHP array containing the code of the ISO 3166-2 country subdivisions
Apple, PHP Add comments
It’s not that easy to find a ISO 3166-2 country region code file. After some Google searches, I ended up on a Debian page with the ISO codes. The thing requires Git. As I will install it soon or later on my Mac OSX, I decided it was the good moment and I installed Git with an installer. I’m not that much a command line guy
Then I retrieved the git repository of the Debian page:
git clone git://git.debian.org/git/iso-codes/iso-codes.git
After 10 minutes, I got all the stuff, and found a file of 300Ko (iso-codes/iso_3166_2/iso_3166_2.xml). Note that they also have different translation for it, they did a good job at Debian.
Finally I just needed a little PHP script. I quickly found one about the country codes and modified it to support regions:
<?php
$filecontent = file_get_contents('iso_3166_2.xml');
$xml = new SimpleXMLElement($filecontent);
$out = "<?php
$regions = array();
";
foreach ($xml->iso_3166_country as $country)
{
foreach ($country->iso_3166_subset as $regions) {
foreach ($regions->iso_3166_2_entry as $region) {
$out .= "\$regions['".$country->attributes()->code."']['".$region->attributes()->code."']='"
.addslashes($region->attributes()->name)."';";
}
}
}
file_put_contents('isoregionnames.php', $out);

Recent Comments