Jump to content

Too many commas in a CSV file


stevepatd

Recommended Posts

I'm trying to parse a csv file.  The user sometimes enters commas into a field in Excel.  Excel will then put quotes around that field that has commas in it when creating the csv file.  Now my fields are all off because of these user entries with commas.  I tried using an fgetcsv thinking it may handle this situation but I get an error with an "Array to string conversion" .  How do I manipulate these lines that have commas within quotes in a csv file?

 

$student = trim(fgets($fh));

$line = explode(",",$student); 

 

 

Link to comment
https://forums.phpfreaks.com/topic/223306-too-many-commas-in-a-csv-file/
Share on other sites

example of fgetcsv from manual:

 

$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
}
fclose($handle);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.