Edit report at https://bugs.php.net/bug.php?id=43225&edit=1
ID: 43225 Updated by: ahar...@php.net Reported by: ed at bronto dot com Summary: fputcsv incorrectly handles cells ending in \ followed by " -Status: Open +Status: Assigned Type: Feature/Change Request Package: Filesystem function related Operating System: Centos PHP Version: 5.2.4 -Assigned To: +Assigned To: aharvey Block user comment: N Private report: N New Comment: I've found the cause of this while writing tests for PR 197. php_fputcsv(), while iterating over the fields to be output, has this fairly odd "escaped" concept â once escape_char (which is hardcoded \ at present) is seen, escaping stops until the next enclosure (" by default) is seen. It doesn't matter whether it's the following character or not. After that, escape_char is then ignored anyway, and the enclosure is used as the "escape" character. This came in via https://github.com/php/php-src/commit/af0adbed3963cdee1bfaf5e3a74b029d2b92c8b7 seven years ago to make the use of enclosures optional â the feature in general is good, but this is definitely an issue in the implementation. Previous Comments: ------------------------------------------------------------------------ [2009-10-09 19:57:18] mbest at icontact dot com magical...@php.net is wrong. This bug is not about fgetcsv but about fputcsv. fputcsv should always escape a double quote to two double quotes. But it doesn't do so if the field contains \" This will mess up the CSV output such that it will not be importable in Excel or other such programs. ------------------------------------------------------------------------ [2009-01-19 12:54:38] magical...@php.net This bug is the same as bug #38918 and bug #38929. * fputcsv() does escape values (replacing " with "", for example) * It seems that fgetcsv() accepts two incompatible unescaping methods Reproduced: php > $fp = fopen('php://temp', 'r'); php > fputcsv($fp, array('foo', 'bar\\', 'baz')); php > rewind($fp); php > echo fgets($fp); foo,"bar\",baz php > rewind($fp); php > var_dump(fgetcsv($fp)); array(2) { [0]=> string(3) "foo" [1]=> string(10) "bar\",baz " } php > echo PHP_VERSION; 5.2.6-pl7-gentoo php > I believe this problem is due to the fact fgetcsv() accept two escaping methods. An extra argument to fgetcsv() could (maybe?) fix this (and the extra argument could be added to fputcsv too) ------------------------------------------------------------------------ [2008-04-17 01:00:55] dan at expireddomain dot com Same problem on windows XP PHP version 5.2.5 on cells that contain a \ followed by double quotes (") ------------------------------------------------------------------------ [2007-11-09 14:59:11] ed at bronto dot com Description: ------------ Using fputcsv to output a cell that ends with a \ followed by double quotes (") causes it to not use any escape sequence. Oddly, fgetscsv is able to parse it correctly. Unlike fgetscsv, I assume fputcsv follows RFC 4180 and uses " as the escape character. Reproduce code: --------------- $row = array(); $row[] = 'a\\"'; $row[] = 'bbb'; $fp = fopen('test.csv', 'w+'); fputcsv($fp, $row); fclose($fp); Expected result: ---------------- expected output: "a\""",bbb Actual result: -------------- actual output: "a\"",bbb ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=43225&edit=1