使用PHP处理国家名称的.txt文件时会丢失字符编码


character encoding is lost when processing a .txt file of country names with with PHP

我正在处理ISO-3166中包含国家名称的文本文件,以便仅将国家名称提取到数组中。问题是当我输出数组时,某些国家的特殊字符丢失或更改:

$country_1 = fopen("./country_names_iso_3166.txt", "r");        
while( !feof($country_1) ) {  // go through every line of file
$country = fgets( $country_1 );
if( strstr($country, ",") )         // if country name contains a comma
    $i = strpos( $country, "," ); // index position of comma
else
    $i = strpos( $country, ";" );   // index position of semicolon
$country = substr( $country, 0, $i );   // extract just the country name
$countries[] = $country;
}

所以现在当我输出数组时,例如,第二个国家名称应该是ÅLAND ISLANDS,但是它输出为LAND ISLANDS…

尝试使用多字节感知字符串函数。mb_strstr(), mb_strpos(), mb_substr()(基本上只是前缀mb_)

请确保输出数据的流使用与输入文件相同的字符集。

(删除说ISO-3166是字符集的错误)