8

我使用此代码从 CSV 文件中获取列数:

$this->dummy_file_handler = fopen($this->config['file'],'r');
if ($dataset =fgetcsv($this->dummy_file_handler))
{
    $this->number_of_columns = count($dataset);
}

除非使用 Excel for Mac 2011 导出文件,否则它工作正常,因为换行符是 无法识别的经典 Mac (CR) 。fgetcsv

alt text

If I manually change the newline from Classic Mac (CR) to Unix (LR), then it works, but I need this to be automated.

How can I make fgetcsv recognize the Classic Mac (CR) new line character?

4

2 回答 2

20

手册

注意:如果在读取 Macintosh 计算机上或由 Macintosh 计算机创建的文件时 PHP 不能正确识别行尾,启用 auto_detect_line_endings 运行时配置选项可能有助于解决问题。

于 2011-01-21T14:27:17.607 回答
1

如果 Saul 的回答不起作用,我会编写一个简单的脚本来一次读取所有文件并将所有 \r 替换为 \n,将结果转储到一个新文件中,然后 fgetcsv'ing 那个新文件。

我觉得这些术语来自使用打字机的日子很有趣:
\n = Line Feed(LF) - 将纸张前进一行。
\r = 回车 (CR) - 将回车返回到打字机的左侧。

于 2011-01-21T14:35:33.787 回答