CSV-parsing by PHP |
The following table is generated by a small PHP script. It shows how the native PHP-function fgetcsv() parses a CSV file (PHP 8.1.32-bookworm.0). The behavior you see below corresponds 100% to the CSV-parsing of Excel 97.
|
# |
CSV |
Value 1 |
Value 2 |
Value 3 |
Value 4 |
1 |
a,b,c |
a |
b |
c |
2 |
"a",b,c |
a |
b |
c |
3 |
'a',b,c |
'a' |
b |
c |
4 |
a , b , c |
a |
b |
c |
5 |
aa,bb;cc |
aa |
bb;cc |
6 |
|
|
7 |
a |
a |
8 |
,b, |
|
b |
|
9 |
,,c |
|
|
c |
10 |
,, |
|
|
|
11 |
"",b |
|
b |
12 |
" ",b |
|
b |
13 |
"a,b" |
a,b |
14 |
"a,b",c |
a,b |
c |
15 |
" a , b ", c |
a , b |
c |
16 |
a b,c |
a b |
c |
17 |
a"b,c |
a"b |
c |
18 |
"a""b",c |
a"b |
c |
19 |
a""b,c |
a""b |
c |
20 |
a,b",c |
a |
b" |
c |
21 |
a,b"",c |
a |
b"" |
c |
22 |
a,"B: ""Hi, I'm B""",c |
a |
B: "Hi, I'm B" |
c |
|
However (not shown in the table), there are some more or less criminal cases of not-so-well-formedness, where PHP and Excel differ. Here's one of them. How would you decide?
' ??? skip the b, or keep it, or keep the quote AND the b, or what ???
' PHP 4.0.6: "a"b,c -> a,c
' Excel 97: "a"b,c -> ab,c
' ParseCSV01: "a"b,c -> a"b,c
|
|