我使用fputcsv制作CSV文件供外部服务提供商导入,我遇到了PHP不使用正确的换行符的问题,如果你将它保留为默认值,它只使用/ n代替/ r / n我找不到改变这个的方法。我通过在行尾使用PHP_EOL来强制执行所需的换行并认为一切正常。事实证明它是在/ r / n换行符周围放置双引号,然后在它之后放入/ n ......
以下是代码:
$csv[$i]['Station ID']=$station_ID;
$csv[$i]['Program Start Date']=$date;
$csv[$i]['Program Start Time']=$start_time;
$csv[$i]['Program End Time']=$end_time;
$csv[$i]['Duration']=$dur;
$csv[$i]['Program Title']=$program;
$csv[$i]['Program Description (optional)']=$desc;
$csv[$i]['Category']=$genre;
$csv[$i]['Sub Category']=$subgenre;
$csv[$i]['Time Zone?']=$timezone;
$csv[$i][]=PHP_EOL;
}
$prev_program = $program ;
unset($gendesc, $desc, $dur, $gendur, $end, $end_time, $start, $start_time, $end_time);
$i++;
}
// create a file pointer connected to the output stream
$output = fopen("php://output",'w') or die("Can't open php://output");
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header("Content-Disposition: attachment; filename='" . $filename . "'");
// output the column headings
fputcsv($output, array('Station ID', 'Program Start Date', 'Program Start Time', 'Program End Time', 'Duration', 'Program Title', 'Program Description (optional)', 'Category', 'Sub Category', 'Time Zone?',PHP_EOL));
// loop over the rows, outputting them
foreach ($csv as $line) {
$line = preg_replace('/(^|;)"([^"]+)";/','$1$2;',$line);
fputcsv($output, $line);
}
fclose($output) or die("Can't close php://output");
}
?>
,这是前两行输出:
"Station ID","Program Start Date","Program Start Time","Program End Time",Duration,"Program Title","Program Description (optional)",Category,"Sub Category","Time Zone?","
"
36170,03/27/17,00:00,00:30,30,"Love Food","Love Food.",Other,Food,Atlantic,"
"
如果我根据记事本拉出PHP_EOL它输出的文本blob(我必须使这个易读,以便用户可以在发送之前根据需要进行验证/修改)但在代码编辑器中似乎是正确的。有没有办法在fputcsv中将默认的EOL语句从/ n更改为/ r / n?我只能参考这个,我可以找到一个来自2008年的补丁的描述,除了所有这一切是将它改为系统默认值,并且它运行在默认为/ n的linux系统上。