瑞典csv文件编码问题


Swedish csv file encoding problems

瑞典字母

我尝试用PHP函数fgetcsv读取csv文件,但我得到编码问题和特殊字符不能正确解释。

我用fopen ($filePath, "r")打开文件,我不指定PHP中我所知道的任何地方的任何编码。关于编码,我的应用程序中的其他一切都工作得很好。

当我在open office套件中打开目标csv文件时,我可以选择编码。如果我选择Unicode(UTF-8),则不能显示特殊字符。如果我选择一些ISO-8859,字母会正确显示。

我一直在玩utf8_decode, utf8_encode, mb_convert_encoding, iconv和setlocale没有运气。

我知道编码是什么,但我不明白这种情况。如果有一个解决方案和一个很好的解释,那就太好了。

我猜我的文件是ISO-8859-*编码的

我如何正确解析文件,以便我可以使用它的内容PHP吗?

Try this
    Å
    Å
    å
    å
    Ä
    Ä
    ä
    ä
    Ö
    Ö
    ö
    ö

您可以对文件进行编码,例如使用htmlentities。

例如,用这一小段代码,我将瑞典文件编码为ISO-8859-1,
$file = fopen("translations-sv.csv", "r");
$new_file = fopen("file_encoded.csv", "w");
while(!feof($file)) {
$line=fgets($file);
$line = str_replace(";", ",",$line);  //replace all ';' to ','
$encoded_line=htmlentities($line,ENT_QUOTES,'ISO-8859-1');
fwrite($new_file, $encoded_line);
}
fclose($file);
fclose($new_file);

Swedish.csv

title_orders;Beställningar
title_monthly_sales;Månadsförsäljning
title_settings;Inställningar

file_encoded.csv

title_orders,Beställningar
title_monthly_sales,Månadsförsäljning
title_settings,Inställningar

比较
$new_file = fopen("file_encoded.csv", "r");
$word_to_find="Orderslutförande";
while (!feof($new_file) ) {
    $line_of_text = fgetcsv($new_file, 1024,",");
if($word_to_find==$line_of_text[1]) 
 echo $line_of_text[1]." is the same to $word_to_find<br>";
}
fclose($new_file);