php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38301 field enclosure behavior in fputcsv
Submitted: 2006-08-03 00:17 UTC Modified: 2018-12-15 13:42 UTC
Votes:15
Avg. Score:4.1 ± 1.1
Reproduced:11 of 11 (100.0%)
Same Version:3 (27.3%)
Same OS:8 (72.7%)
From: programmer at tklee dot com Assigned: cmb (profile)
Status: Closed Package: Filesystem function related
PHP Version: 5.1.4 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: programmer at tklee dot com
New email:
PHP Version: OS:

 

 [2006-08-03 00:17 UTC] programmer at tklee dot com
Description:
------------
Regarding the field enclosure parameter in fputcsv...

1. It's unrealistic to require the field enclosure to be one character because it's very common to have "empty string" as the field delimiter (especially when TAB is used as field delimiter).  I tried to use "\0" as the field enclosure, hoping that'd be interpreted as an empty string, but fputcsv translated it into literal.

2. fputcsv wrongly adds the field enclosures whenever a field contains a space. The expected behavior should be adding the field enclosures when a field contains a field delimiter.


Reproduce code:
---------------
/*
test_in.csv has only one line:
$line = "field 0\tfield_1\tfield 2\n";
*/

$fh_in=fopen("test_in.csv","r");
$fh_out=fopen("test_out.csv","w");

// since "" is not accepted as the 4th parameter, I use "\0" instead
$fields = fgetcsv($fh_in, 0, "\t", "\0");
fputcsv($fh_out, $fields, "\t", "\0");

close($fh_in);
close($fh_out);

Expected result:
----------------
/*
One would expect to see in test_out.csv :
$line = "field 0\tfield_1\tfield_2\n";
*/

Actual result:
--------------
/*
However, the result shows:
$line = "\0field 0\0\tfield_1\t\0field 2\0\n";

Unexpected:
1. Since space is not the field delimiter, there is no point of using the field enclosure.

2. Empty enclosure is very common and should be accepted.
*/

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-02 23:57 UTC] datibbaw@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2014-12-02 23:57 UTC] datibbaw@php.net
As far as expected behaviour goes, RFC 4180 states:

> Each field may or may not be enclosed in double quotes

Also, there's a provision that states:

> Spaces are considered part of a field and should not be ignored.

That said, while I agree that spaces (or tabs, if not the delimiter) in a field do not strictly require an enclosure (unlike newlines or the enclosure character itself), the only thing coming close to a standard doesn't forbid it.
 [2014-12-04 08:09 UTC] programmer at tklee dot com
Wow! After 8.5 years! That's dedication!

Thank you for your answer to issue 2.

If we can address issue 1, then issue 2 can be solved at the same time. Allowing "" to be the quote char is actually a closer implementation of RFC 4180. Users can choose whether or not to enclose the field with something.

In short, can we accept "" as the 4th parameter of fputcsv?

Thanks for reconsidering.
 [2018-02-11 17:55 UTC] cmb@php.net
-Package: *General Issues +Package: Filesystem function related
 [2018-09-13 12:36 UTC] cmb@php.net
<https://github.com/php/php-src/pull/3515> would implement this
feature request.
 [2018-12-15 13:42 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-12-15 13:42 UTC] cmb@php.net
Implemented as commit 3b0f051[1]; available as of PHP 7.4.0.

[1] <http://git.php.net/?p=php-src.git;a=commit;h=3b0f05119383fe21ee75adaed3d0239ba8976aef>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC