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
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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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