parsecsv / parsecsv-for-php

CSV data parser for PHP.
MIT License
678 stars 176 forks source link

Field containing comma creates an parsing issue while using the save() function #177

Open romainlaisne opened 5 years ago

romainlaisne commented 5 years ago

Hi,

I am parsing a php array to csv using the save() function of this library.

I get an parsing issue, the fields are not parse correctly, when one of the field in my php array contains an comma, which is also the field delimiter in my case.

Here is an example of the CSV output ,"""9","72""",,newsletter,,abos_speedresa,Monsieur,"free travel"

See that the value was 9,72 and it has been strangely quoted! "free travel" has been correctly parsed with added quote however.

If anyone has an idea how to fix this?

Thanks in advance

gogowitsch commented 5 years ago

@romainlaisne Thanks for posting your problem here. I believe this is how CSV should work with quotes contained within cells.

I just wrote a test to see what would happen and found nothing surprising:

    public function testSaveWithDelimiterOfComma() {
        $this->csv = new Csv();
        $this->csv->heading = false;
        $this->csv->delimiter = ",";
        $this->csv->linefeed = "\n";
        $this->csv->data = [
            [
                '3,21',
                'newsletter',
                'Monsieur',
                'free travel',
            ],
            [
                '"9,72"',
                'newsletter',
                'Madame',
                '"free travel"',
            ],
        ];
        $expected =
            '"3,21",newsletter,Monsieur,free travel' . "\n" .
            '"""9,72""",newsletter,Madame,"""free travel"""' . "\n";
        $actual = $this->csv->unparse();
        self::assertSame($expected, $actual);
    }

Both Excel 2016 and Open Office 4.1.3 opened that correctly. For example:
image

romainlaisne commented 5 years ago

Thanks for your reply and setting up a test. But, my php array contained 9,72 only (with no quotes) and it has been converted to : """9","72""" That is not correct, is it?

An extra info, I use version 1.0 of the library. Perhaps something has been corrected since then?

Thanks

gogowitsch commented 5 years ago

Could you paste your code or source data? This particular behavior was not changed since the 1.0 release.

romainlaisne commented 4 years ago

Hi,

Concerning code, I am using save() function just like this:

$parseCsvService->save($temp_csv_path, $csv_data, $append, $csv_header);

I see you use unparse() directly in your test. Perhaps a difference here ? Even though I see the save() function use also unparse().

To test, we can just pass these arrays to the save() function :

$csv_data=[['3,21']];

and a header :

$csv_header=array("header1");
gogowitsch commented 4 years ago

@romainlaisne Your last example wrote a file with this content:

header1
"3,21"

I checked with version 1.0.0 and with the current master. The results are the same.

To me, this looks perfect and expected. What did you expect?

stevleibelt commented 2 years ago

@romainlaisne is your issue solved now? This ticket is pretty damn old and it would be nice to close it with a remark of success.

Cheers, Stev