fputcsv()

php.internals

David Sklar

22 years ago
Attached is my fputcsv() patch (and a test case) from April. Could someone commit this to 5.1 or 5.0.1? Thanks, David

Marcus Börger

22 years ago
Hello David, Wednesday, August 11, 2004, 3:53:11 PM, you wrote:
> Attached is my fputcsv() patch (and a test case) from April. Could > someone commit this to 5.1 or 5.0.1?
That's a new function so it can only go to 5.1. Anyway it's a nice feature. So if nobody has anything against i'd look into it more detailed later this week and commit it. Thanks David. regards marcus

Marcus Börger

22 years ago
Hello David, as promised i looked at the patch. Besides a small misstake it looks good. But it seems to work different then fgetcsv(). In other words if you write using fputcsv() you are not sure to get the same back with fgetcsv(). Though my current opinion is that fputcsv() is doing it right. The attached patch includes a testfile that demonstrates the problem. anyone having thoughts? Wednesday, August 11, 2004, 8:40:07 PM, me wrote:
> Hello David,
> Wednesday, August 11, 2004, 3:53:11 PM, you wrote:
>> Attached is my fputcsv() patch (and a test case) from April. Could >> someone commit this to 5.1 or 5.0.1?
> That's a new function so it can only go to 5.1. Anyway it's a nice feature. > So if nobody has anything against i'd look into it more detailed later this > week and commit it. Thanks David.
> regards > marcus
-- Best regards, Marcus mailto:helly@php.net

David Sklar

22 years ago
Marcus Boerger wrote:
> Hello David, > > as promised i looked at the patch. Besides a small misstake it looks > good. But it seems to work different then fgetcsv(). In other words if > you write using fputcsv() you are not sure to get the same back with > fgetcsv(). Though my current opinion is that fputcsv() is doing it right. > The attached patch includes a testfile that demonstrates the problem. > > anyone having thoughts?
I agree that if you write something with fputcsv() and then read it with fgetcsv() you should get back what you started with. I don't see what's broken though. (Maybe I am misunderstanding your test cases.) The original array in the test file is:
> +$list = array ( > + 0 => 'aaa,bbb', > + 1 => 'aaa,"bbb"', > + 2 => '"aaa","bbb"', > + 3 => 'aaa,bbb', > + 4 => '"aaa",bbb', > + 5 => '"aaa", "bbb"', > + 6 => ',', > + 7 => 'aaa,', > + 8 => ',"aaa"', > + 9 => '"",""', > + 10 => '"""""",', > + 11 => '""""",aaa', > + 12 => 'aaa,bbb ', > + 13 => 'aaa,"bbb "', > + 14 => 'aaa"aaa","bbb"bbb', > + 15 => 'aaa"aaa""",bbb', > + 16 => 'aaa,"\\"bbb,ccc', > + 17 => 'aaa"\\"a","bbb"', > + 18 => '"\\"","aaa"', > + 19 => '"\\""",aaa',
And then what gets printed out by fputcsv() is this: (which is different, but that's OK, since fputcsv() has worked its encoding magic):
> +$list = array ( > + 0 => 'aaa,bbb', > + 1 => 'aaa,"""bbb"""', > + 2 => '"""aaa""","""bbb"""', > + 3 => 'aaa,bbb', > + 4 => '"""aaa""",bbb', > + 5 => '"""aaa"""," ""bbb"""', > + 6 => ',', > + 7 => 'aaa,', > + 8 => ',"""aaa"""', > + 9 => '"""""",""""""', > + 10 => '"""""""""""""",', > + 11 => '"""""""""""",aaa', > + 12 => 'aaa,"bbb "', > + 13 => 'aaa,"""bbb """', > + 14 => '"aaa""aaa""","""bbb""bbb"', > + 15 => '"aaa""aaa""""""",bbb', > + 16 => 'aaa,"""\\""bbb",ccc', > + 17 => '"aaa""\\""a""","""bbb"""', > + 18 => '"""\\""""","""aaa"""', > + 19 => '"""\\""""""",aaa', > +);
And then this is the array after being read in by fgetcsv():
> +$list = array ( > + 0 => 'aaa,bbb', > + 1 => 'aaa,"bbb"', > + 2 => '"aaa","bbb"', > + 3 => 'aaa,bbb', > + 4 => '"aaa",bbb', > + 5 => '"aaa", "bbb"', > + 6 => ',', > + 7 => 'aaa,', > + 8 => ',"aaa"', > + 9 => '"",""', > + 10 => '"""""",', > + 11 => '""""",aaa', > + 12 => 'aaa,bbb ', > + 13 => 'aaa,"bbb "', > + 14 => 'aaa"aaa","bbb"bbb', > + 15 => 'aaa"aaa""",bbb', > + 16 => 'aaa,"\\"bbb,ccc', > + 17 => 'aaa"\\"a","bbb"', > + 18 => '"\\"","aaa"', > + 19 => '"\\""",aaa', > +);
Which seems to be the same as the original array? David

Marcus Börger

22 years ago
Hello David, i looked up the fputcsv() magic in the file manually and agree that this is fairly correct (as i wrote before). But when i execute the test the second var_export() is supposed to show the input from the .phpt file. Unfortunatley i get a FAIL from run-tests.php: FAIL various fputcsv() functionality tests [ext/standard/tests/file/fputcsv.phpt] 040+ 16 => 'aaa,"\\"bbb",ccc', 041+ 17 => 'aaa"\\"a""","bbb"', 042+ 18 => '"\\""","aaa"""', 043+ 19 => '"\\"""",aaa 044+ ', 040- 16 => 'aaa,"\\"bbb,ccc', 041- 17 => 'aaa"\\"a","bbb"', 042- 18 => '"\\"","aaa"', 043- 19 => '"\\""",aaa',marcus@frodo /usr/src/php-cvs $ Care to look into this (done with current HEAD)? best regards marcus Thursday, September 2, 2004, 6:17:51 PM, you wrote:
> Marcus Boerger wrote:
>> Hello David, >> >> as promised i looked at the patch. Besides a small misstake it looks >> good. But it seems to work different then fgetcsv(). In other words if >> you write using fputcsv() you are not sure to get the same back with >> fgetcsv(). Though my current opinion is that fputcsv() is doing it right. >> The attached patch includes a testfile that demonstrates the problem. >> >> anyone having thoughts?
> I agree that if you write something with fputcsv() and then read it with > fgetcsv() you should get back what you started with.
> I don't see what's broken though. (Maybe I am misunderstanding your test > cases.) The original array in the test file is:
>> +$list = array ( >> + 0 => 'aaa,bbb', >> + 1 => 'aaa,"bbb"', >> + 2 => '"aaa","bbb"', >> + 3 => 'aaa,bbb', >> + 4 => '"aaa",bbb', >> + 5 => '"aaa", "bbb"', >> + 6 => ',', >> + 7 => 'aaa,', >> + 8 => ',"aaa"', >> + 9 => '"",""', >> + 10 => '"""""",', >> + 11 => '""""",aaa', >> + 12 => 'aaa,bbb ', >> + 13 => 'aaa,"bbb "', >> + 14 => 'aaa"aaa","bbb"bbb', >> + 15 => 'aaa"aaa""",bbb', >> + 16 => 'aaa,"\\"bbb,ccc', >> + 17 => 'aaa"\\"a","bbb"', >> + 18 => '"\\"","aaa"', >> + 19 => '"\\""",aaa',
> And then what gets printed out by fputcsv() is this: (which is > different, but that's OK, since fputcsv() has worked its encoding magic):
>> +$list = array ( >> + 0 => 'aaa,bbb', >> + 1 => 'aaa,"""bbb"""', >> + 2 => '"""aaa""","""bbb"""', >> + 3 => 'aaa,bbb', >> + 4 => '"""aaa""",bbb', >> + 5 => '"""aaa"""," ""bbb"""', >> + 6 => ',', >> + 7 => 'aaa,', >> + 8 => ',"""aaa"""', >> + 9 => '"""""",""""""', >> + 10 => '"""""""""""""",', >> + 11 => '"""""""""""",aaa', >> + 12 => 'aaa,"bbb "', >> + 13 => 'aaa,"""bbb """', >> + 14 => '"aaa""aaa""","""bbb""bbb"', >> + 15 => '"aaa""aaa""""""",bbb', >> + 16 => 'aaa,"""\\""bbb",ccc', >> + 17 => '"aaa""\\""a""","""bbb"""', >> + 18 => '"""\\""""","""aaa"""', >> + 19 => '"""\\""""""",aaa', >> +);
> And then this is the array after being read in by fgetcsv():
>> +$list = array ( >> + 0 => 'aaa,bbb', >> + 1 => 'aaa,"bbb"', >> + 2 => '"aaa","bbb"', >> + 3 => 'aaa,bbb', >> + 4 => '"aaa",bbb', >> + 5 => '"aaa", "bbb"', >> + 6 => ',', >> + 7 => 'aaa,', >> + 8 => ',"aaa"', >> + 9 => '"",""', >> + 10 => '"""""",', >> + 11 => '""""",aaa', >> + 12 => 'aaa,bbb ', >> + 13 => 'aaa,"bbb "', >> + 14 => 'aaa"aaa","bbb"bbb', >> + 15 => 'aaa"aaa""",bbb', >> + 16 => 'aaa,"\\"bbb,ccc', >> + 17 => 'aaa"\\"a","bbb"', >> + 18 => '"\\"","aaa"', >> + 19 => '"\\""",aaa', >> +);
> Which seems to be the same as the original array?
> David
-- Best regards, Marcus mailto:helly@php.net

David Sklar

22 years ago
Marcus Boerger wrote:
> Hello David, > > i looked up the fputcsv() magic in the file manually and agree that this > is fairly correct (as i wrote before). But when i execute the test the > second var_export() is supposed to show the input from the .phpt file. > Unfortunatley i get a FAIL from run-tests.php: > > FAIL various fputcsv() functionality tests [ext/standard/tests/file/fputcsv.phpt] > 040+ 16 => 'aaa,"\\"bbb",ccc', > 041+ 17 => 'aaa"\\"a""","bbb"', > 042+ 18 => '"\\""","aaa"""', > 043+ 19 => '"\\"""",aaa > 044+ ', > 040- 16 => 'aaa,"\\"bbb,ccc', > 041- 17 => 'aaa"\\"a","bbb"', > 042- 18 => '"\\"","aaa"', > 043- 19 => '"\\""",aaa',marcus@frodo /usr/src/php-cvs $ > > Care to look into this (done with current HEAD)?
I will poke about in the guts of fgetcsv and see what I can find. There have been some changes to fgetcsv() in the PHP_4_3 branch, perhaps they will fix things. David

Marcus Börger

22 years ago
Hello David, it is obviously fgetcsv(), see last comment on #29719 [Opn]: fgetcsv - double quotes issue marcus Thursday, September 2, 2004, 10:26:18 PM, you wrote:

Curt Zirzow

22 years ago
* Thus wrote Marcus Boerger:
> Hello David, > > as promised i looked at the patch. Besides a small misstake it looks > good. But it seems to work different then fgetcsv(). In other words if > you write using fputcsv() you are not sure to get the same back with > fgetcsv(). Though my current opinion is that fputcsv() is doing it right. > The attached patch includes a testfile that demonstrates the problem. > > anyone having thoughts?
i remember seeing something a while back when I first looked at writing this function that low ascii (<32) and higher (>97) ascii chars constitute a quoted entry. Unfortuantely I cant refind any of the resources that suggested this behaviour. I'm just throwing this out in case anyone can confirm this. Curt
-- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid!