fputcsv and Notepad

2019-07-01 10:17发布

I generate csv files with fputcsv and it works just fine, but when I view the csv file in Notepad (Windows) there is no new lines. All lines are in just 1 line, and where the linebreak is supposed to be, there is a sqaure (corrupted character). Other editors on Mac for example show the lines with breaks correctly.

Is there any way to fix this problem as the CSV needs to be imported, and the importer does not see the linebreaks as well.

标签: php csv
3条回答
做个烂人
2楼-- · 2019-07-01 11:05

Try using Notepad++ which handles \n newlines more correctly.

Also, see this response inn fputcsv documentation: http://lv.php.net/manual/en/function.fputcsv.php#90883

查看更多
▲ chillily
3楼-- · 2019-07-01 11:15

http://bugs.php.net/bug.php?id=46367

This is a known and unfixed bug in PHP.

Until they fix it, try using fwrite($fp, '"'.implode('","', str_replace('"', '""', $data_array)).'"'.PHP_EOL);

查看更多
甜甜的少女心
4楼-- · 2019-07-01 11:22

put \r\n to end of each line in your code.

Other Quick Fix: Open your generated file in WordPad, it should show them fine, press Cntr+S and now open in notepad, it should show fine in that too.

Edit:

Based on the comments below, just modify your code as follows:

$orders_newlines = array();

foreach($orders as $value)
{
  $orders_newlines[] = $value . "\r\n";

}

Now use $orders_newlines variable instead of $orders in your loop.

Hope that helps.

查看更多
登录 后发表回答