thephpleague/csv

Enclosure not works properly

JiRysavy opened this issue · 2 comments

Bug Report

Enclosure around words is not removed

| 9.14 | |
| 8.1 | |
| Docker on Ubuntu 22.04|

I have some trouble with reading cells data surrounded with ".

I have CSV that looks something like:
"h1";"h2";"h3";"h4";"h5";
"d1";"d2";"d3";"d4";"d5";
"l1";"l2";"l3";"l4";"l5";

and code:
$csv = Reader::createFromPath($filePath, 'rb'); $csv->setHeaderOffset(0) ->setDelimiter(';') ->setEnclosure('"');

I expect, the getResults return to be:
[
[
'h1' => 'd1',
'h2' => 'd2',
'h3' => 'd3',
'h4' => 'd4',
'h5' => 'd5',
],
[
'h1' => 'l1',
'h2' => 'l2',
'h3' => 'l3',
'h4' => 'l4',
'h5' => 'l5',
]
], but actual result is:

[
[
'"h1"' => '"d1"',
'"h2"' => '"d2"',
'"h3"' => '"d3"',
'"h4"' => '"d4"',
'"h5"' => '"d5"',
],
[
'"h1"' => '"l1"',
'"h2"' => '"l2"',
'"h3"' => '"l3"',
'"h4"' => '"l4"',
'"h5"' => '"l5"',
]
],

It's actually a feature.

Single quotes are not formally specified in RFC 4180 for enclosing fields, hence their behavior can be inconsistent between different parsers.

@JiRysavy this is an expected behaviour in how arrays are being presented in PHP. The enclosure you see are part of array representation but not par of your CSV document.

@codespearhead the behaviour has nothing to do with RFC4180.