csv: League\Csv\Writer - Invalid CSV
Issue summary
A string with a doubled backslash at the end, leads to an invalid CSV file. It looks like that in the CSV File:
"a text string \\"", "..."
System informations
| Information | Description |
|---|---|
| League\Csv version | 9.1.4 |
| PHP/HHVM version | PHP 7.2 |
| OS Platform | Debian GNU/Linux 9 |
Standalone code, or other way to reproduce the problem
Code not tested, just written to make it more easy to understand the problem:
$csvWriter = Writer::createFromPath('/tmp/file.csv', 'a+');
$csvWriter->setNewline("\r\n"); //RFC4180 Line feed
RFC4180Field::addTo($csvWriter);
$line=['a text string \\','...'];
$csvWriter->insertOne($line);
Expected result
"a text string \\", "..."
Actual result
"a text string \\"", "..."
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 20 (12 by maintainers)
Commits related to this issue
- bug fix #307 — committed to thephpleague/csv by nyamsprod 6 years ago
There is now a pull request to fix this in PHP: https://github.com/php/php-src/pull/3515.
yes there’s one
fputcsvadds enclosures in presence of the white spaces in a record field, RFC4180 does not.But to me that’s a minor issue since having more enclosed field gives less error prone conversion than the other way around (not having enclosure at all anytime around record fields).
@theodorejb the following PR #309 contains the fix TL;DR it falls back to using
fwriteinstead offputcsv.Indeed if the upstream code in
fputcsvwas patch I would no longer need the optional parameter. But since I can not 100% guarantee that this is how the patch will land (using an empty string) I’d rather not rely on a fix which has not yet land, what if the patch usesnullorfalseinstead of the empty string ?The issue is not with how the patch works but how it is triggered. Should it be triggered by a special input for the escape character OR should it be triggered by a optional argument on the insertXXX methods.
IMO using an optional argument is the best approach for the following reason If PHP was to fix the escape parameter bug
Writer::MODE_RFC4180relying onfwrite(see #309);fputcsv;League\Csvis released which switch internally tofputcsvon all modes but still usesfwritefor legacy PHP version;Writer::MODE_RFC4180constant and just relies onfputcsvbehavior;This is clean simple and reliable. On the other hand if we use the “escape” parameter switch and the PHP patch is done differently (let’s hope not 😄 ) then we will have to directly release a v10 because of potential bugs. Not to mention that nothing is said for
fgetcsvwill it be patch too to ignore the escape parameter using the same empty string 🤔I’ve seen your post on the PHP internal mailing list 👍 . Let’s hope this boost the release of a patch quickly!!
I suppose that benchmark using
fwritedecrease insertion speed, but it should depend on the record content length 🤔 You can benchmark the PR to see the difference.@nyamsprod It was just a additional thought from me - i think your solution is valid in terms of a coding view.