Skip to content

Instantly share code, notes, and snippets.

@LastDragon-ru
Created December 27, 2013 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LastDragon-ru/8143002 to your computer and use it in GitHub Desktop.
Save LastDragon-ru/8143002 to your computer and use it in GitHub Desktop.
Convert ISO 3166-1 Alpha-2 into php code
<pre>
<?php
/**
* @see http://www.iso.org/iso/home/standards/country_codes.htm
* @author Aleksey Lebedev aka LastDragon <LastDragon@yandex.ru>
* @license BSD license
*/
$file = 'http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_txt.htm';
$data = file($file);
$delimiter = ';';
if (!$data || count($data) <= 1) {
echo 'File does not exist or is empty :(';
}
array_shift($data);
foreach ($data as $line) {
list ($name, $code) = str_getcsv($line, $delimiter) + array(null, null);
if ($name && $code) {
$name = addslashes(iconv('UTF-8', 'ASCII//TRANSLIT', $name));
$code = strtoupper($code);
echo "const COUNTRY_{$code} = '{$name}';\n";
}
}
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment