题
我正在使用PHP中的FPUTCSV来输出数据库查询的逗号限制文件。在Ubuntu中的GEDIT打开文件时,它看起来正确 - 每个记录都有一个线路休息(没有可见的换行字符,但是您可以告诉每个记录是分开的,并且在OpenOffice电子表格中打开它,使我可以正确查看文件。
但是,我们将这些文件发送到Windows上的客户端,并且在其系统上,该文件作为一条大而长的线条。在Excel中打开它,它根本无法识别多条线。
我在这里读了几个问题,这些问题非常相似,包括 这个, ,其中包括指向真正有益的链接 伟大的新线分裂 解释。
不幸的是,我们不能仅仅告诉客户在“更聪明”的编辑器中打开文件。他们需要能够在Excel中打开它们。是否有任何程序化方法来确保添加正确的newline字符,以便可以在任何操作系统上的电子表格程序中打开文件?
我已经在使用自定义功能来强制所有值,因为FputCSV对此有选择性。我尝试做这样的事情:
function my_fputcsv($handle, $fieldsarray, $delimiter = "~", $enclosure ='"'){
$glue = $enclosure . $delimiter . $enclosure;
return fwrite($handle, $enclosure . implode($glue,$fieldsarray) . $enclosure."\r\n");
}
但是,当文件在Windows文本编辑器中打开时,它仍然显示为单个长行。
解决方案 8
我最终确实在专家交换中得到了答案。这是有效的方法:
function my_fputcsv($handle, $fieldsarray, $delimiter = "~", $enclosure ='"'){
$glue = $enclosure . $delimiter . $enclosure;
return fwrite($handle, $enclosure . implode($glue,$fieldsarray) . $enclosure.PHP_EOL);
}
用于代替标准FPUTCSV。
其他提示
// Writes an array to an open CSV file with a custom end of line.
//
// $fp: a seekable file pointer. Most file pointers are seekable,
// but some are not. example: fopen('php://output', 'w') is not seekable.
// $eol: probably one of "\r\n", "\n", or for super old macs: "\r"
function fputcsv_eol($fp, $array, $eol) {
fputcsv($fp, $array);
if("\n" != $eol && 0 === fseek($fp, -1, SEEK_CUR)) {
fwrite($fp, $eol);
}
}
这是@John Douthat的好答案的改进版本,保留了使用自定义定系数和外壳以及返回FputCSV的原始输出的可能性:
function fputcsv_eol($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "\n") {
$return = fputcsv($handle, $array, $delimiter, $enclosure);
if($return !== FALSE && "\n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
fwrite($handle, $eol);
}
return $return;
}
使用php函数fputcsv仅写入 n,不能自定义。尽管有些软件包还将检测到Linux Newline,但这使得Microsoft环境的功能一文不值。
FPUTCSV的好处仍然使我挖掘到发送到文件之前替换Newline字符的解决方案。这可以通过将FPUTCSV流到PHP TEMP流中的构建中来完成。然后将newline字符调整到您想要的任何内容中,然后保存到文件。像这样:
function getcsvline($list, $seperator, $enclosure, $newline = "" ){
$fp = fopen('php://temp', 'r+');
fputcsv($fp, $list, $seperator, $enclosure );
rewind($fp);
$line = fgets($fp);
if( $newline and $newline != "\n" ) {
if( $line[strlen($line)-2] != "\r" and $line[strlen($line)-1] == "\n") {
$line = substr_replace($line,"",-1) . $newline;
} else {
// return the line as is (literal string)
//die( 'original csv line is already \r\n style' );
}
}
return $line;
}
/* to call the function with the array $row and save to file with filehandle $fp */
$line = getcsvline( $row, ",", "\"", "\r\n" );
fwrite( $fp, $line);
另外,您可以以本机Unix格式输出(仅 n),然后在结果文件上运行UNIX2DOS,以在适当的位置中转换为 r n。请注意您的数据不包含 n。另外,我看到您正在使用〜的默认分离器。尝试 t的默认分离器。
作为 Webbiedave 指出(THX!)可能是最干净的方法是使用流滤波器。
它比其他解决方案要复杂一些,但即使在写信给它们后无法编辑的流上也可以使用(例如使用下载 $handle = fopen('php://output', 'w');
)
这是我的方法:
class StreamFilterNewlines extends php_user_filter {
function filter($in, $out, &$consumed, $closing) {
while ( $bucket = stream_bucket_make_writeable($in) ) {
$bucket->data = preg_replace('/([^\r])\n/', "$1\r\n", $bucket->data);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
stream_filter_register("newlines", "StreamFilterNewlines");
stream_filter_append($handle, "newlines");
fputcsv($handle, $list, $seperator, $enclosure);
...
我一直在处理类似的情况。这是我发现的解决方案,该解决方案可以输出具有Windows友好线路端的CSV文件。
http://www.php.net/manual/en/function.fputcsv.php#90883
我无法使用该文件,因为我试图将文件流式传输到客户端,并且无法使用FSEEKS。
Windows需要 \r\n
作为 折线/马车 返回组合以显示单独的行。