CSV导出中的æøå

时间:2018-09-07 08:19:34

标签: php csv slim

我正在尝试将一些数据导出到可以下载的CSV文件中。

如果我在邮递员中碰到我的API,则数据看起来像这样:

Notat,Vare,Type,Størrelse,Tekst,Antal,Pris,Rabatprocent,Totalbeløb,Fakturerbar,"Lever nu",Faktureret,Lagersted,Placering,Lagerstyring
,,,,"Fadol 0,5 - 6 stk. ",4,,"0,00",1000,Checked,0,"0,00",,,Intet
,,,,"Classic 0,5",34,,"0,00",1700,Checked,0,"0,00",,,Intet
,,,,"Classic 0,5 - 6 stk ",3,,"0,00",750,Checked,0,"0,00",,,Intet
,,,,"Grimbergen 0,3",6,,"0,00",240,Checked,0,"0,00",,,Intet
,,,,"Nordic flaske ",0,,"0,00",0,Checked,0,"0,00",,,Intet
,,,,Kildevand,109,,"0,00",2160,Checked,0,"0,00",,,Intet
,,,,"Smirnoff 6 stk",0,,"0,00",0,Checked,0,"0,00",,,Intet
,,,,"Sæson 0,3",2,,"0,00",80,Checked,0,"0,00",,,Intet
,,,,"Sodavand 0,3",5,,"0,00",150,Checked,0,"0,00",,,Intet
,,,,"Sodavand 0,5",102,,"0,00",4040,Checked,0,"0,00",,,Intet
,,,,"Fadøl 0,5",20,,"0,00",1000,Checked,0,"0,00",,,Intet
,,,,"Kop varme drikke",17,,"0,00",255,Checked,0,"0,00",,,Intet

但是当我下载后打开CSV文件时,它将显示Størrelse为StÃrrrelse。 Så在邮递员中可以正常使用,但在实际的CSV文件中却不能。有人可以为我指出问题所在的正确方向吗?

这是我创建导出的代码:

//Open file pointer.        
        $fp = fopen('php://output', 'w');

        //Loop through the associative array.
        foreach($sortedData as $row){
            //Write the row to the CSV file.

            fputcsv($fp, $row);
        }

        //Finally, close the file pointer.
        fclose($fp);

        $response = $response->withHeader('Content-Type', 'text/csv; charset=utf-8');
        $response = $response->withHeader('Content-Disposition', 'attachment; filename="file.csv"');

        $stream = fopen('php://memory', 'r+');      

        return $response->withBody(new \Slim\Http\Stream($stream));

1 个答案:

答案 0 :(得分:0)

取决于编码类型。我在csv文件中遇到了类似的问题。我将该行转换为“ ISO-8859-1”(德语字符)。

 iconv(mb_detect_encoding($text), "ISO-8859-1", $text);