How can I make PHP's fgetcsv() to recognize the Classic Mac (CR) new line character?
I use this code to get the number of columns from a CSV f开发者_开发知识库ile:
$this->dummy_file_handler = fopen($this->config['file'],'r');
if ($dataset =fgetcsv($this->dummy_file_handler))
{
$this->number_of_columns = count($dataset);
}
It works fine unless the file is exported with Excel for Mac 2011 since the new line character is then Classic Mac (CR) which fgetcsv
doesn't recognize.
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?
From the manual:
Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.
If Saul's answer doesn't work, I'd write a simple script to read in the file all at once and str_replace all \r with \n, dumping the results into a new file, then fgetcsv'ing that new file.
I find it amusing that these terms come from the days of using typewriters:
\n = Line Feed(LF) - advances the paper one line.
\r = Carriage Return (CR) - returns the carriage to the left side of the typewriter.
精彩评论