Random row breaks in CSV read with php

2019-08-26 08:07发布

I am trying to read and display the content of a CSV file. For some reason it bugs out with this file I am trying to read.

This is my code.

<?php
if (($handle = fopen("http://testdummies.dk/csv/weird2.csv", "r")) !== FALSE) {

while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
    echo "<pre>";
            print_r ($data);
            echo "</pre>";

}
fclose($handle);
}
?>

I use a danish CSV file so it uses ; instead of ,!

I have made this test at http://testdummies.dk/csv/testdups2.php

The first 3 arrays it generates are correct, but the 4th breaks out at "cell" 47. I just cant figure out why. (the file opens fine in excel).

The file is at http://testdummies.dk/csv/weird2.csv

Thanks in advance.

René

2条回答
可以哭但决不认输i
2楼-- · 2019-08-26 08:43

Thanks @Barmar for the answer...from that blog post I found a slightly more recent library built off of the CsvFileParser class, with some additional functions.

ParseCSV

For example the ParseCSV library is built to allow for automatically ignoring the header row in a file. Also the resulting array output seems to be structured a bit more cleanly.

查看更多
smile是对你的礼貌
3楼-- · 2019-08-26 08:48

This blog post says that PHP fgetcsv() doesn't handle quoted newlines properly. He provides his own CsvFileParser class that addresses the problem.

查看更多
登录 后发表回答