Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
romainlaisne opened this issue Sep 16, 2019 · 6 comments

Comments

@romainlaisne
Copy link

romainlaisne commented Sep 16, 2019

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
Copy link
Member

@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
Copy link
Author

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
Copy link
Member

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

@gogowitsch gogowitsch reopened this Sep 18, 2019
@romainlaisne
Copy link
Author

romainlaisne commented Sep 25, 2019

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
Copy link
Member

@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
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants