Search code examples
phpnewlinefgetcsv

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 file:

$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.

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?


Solution

  • 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.