ID:               43225
 Comment by:       mbest at icontact dot com
 Reported By:      ed at bronto dot com
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: Centos
 PHP Version:      5.2.4
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[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 http://bugs.php.net/?id=43225&edit=1

Reply via email to