PHP将文本分解为数组[关闭]

I am trying to convert the contents of this link (only the Country/code list) into an array. To do so I tried using explode(), but exploding on , , , or doesn't work.

Anybody knows what they are using? I only need the country name and the 2 letter-code.

consider using this page instead - http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_txt.htm

or in XML http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_xml.htm

as it will be easier to parse!

To do it programmatically...

$country_arr = [];
$raw = file_get_contents("http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_txt.html");
$lines = explode("
",$raw);
foreach ($lines as &$line) {
    $bits = explode(";",$line);
    $country_arr[$bits[0]] = $bits[1];
}

Your work has already been done:

PHP Array of ISO 3166 Country Codes

If you need to do this only once, copy the text in a notepad, search & replace any unnecessary characters, and then do the explode().

If you need to do it on demand, by code, then program the same search&replace actions by php.

I guess you are using PHP.

preg_match('/^([\w\s]+\w)\s+(\w{2})\s+\w{3}\s+\d{3}\s*$/', $contents, $matches);

You'll get an array with the needed information.

On website www.iso.org you have HTML, Text and XML versions.

Parsing TXT version :

$a = [];
$d = file_get_contents('http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_txt.htm');
foreach (explode("
", trim($d)) as $i => $v) {
    if (!$i) continue;
    $v = explode(";", $v);
    $a[$v[1]] = $v[0];
}
print_r($a);

Parsing XML version :

$a = [];
$d = file_get_contents('http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_xml.htm');
foreach (simplexml_load_string($d) as $v) {
    $a[ (string)$v->{'ISO_3166-1_Alpha-2_Code_element'} ] = (string)$v->{'ISO_3166-1_Country_name'};
}
print_r($a);