Req #42357 [Opn]: add optional parameter for line ending to fputcsv
| From: | cmb@php.net | Date: | Tue, 25 Oct 2016 17:57:58 +0000 |
| Subject: | Req #42357 [Opn]: add optional parameter for line ending to fputcsv | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-205007@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=42357&edit=1
ID: 42357
Updated by: cmb@php.net
Reported by: php at phihag dot de
Summary: add optional parameter for line ending to fputcsv
Status: Open
Type: Feature/Change Request
Package: Filesystem function related
PHP Version: 5.2.4RC2
Block user comment: N
Private report: N
New Comment:
> Small correction of the original bug report: \n is always used,
> [â¦]
Indeed[1], and this is actually a bug. RFC 4180[2] states:
> Each record is located on a separate line, delimited by a line
> break (CRLF).
[1] <https://github.com/php/php-src/blob/PHP-7.0.12/ext/standard/file.c#L1927>
[2] <https://tools.ietf.org/html/rfc4180>
Previous Comments:
------------------------------------------------------------------------
[2007-08-22 16:25:19] php at phihag dot de
No, same behavior (tested on Linux).
From php.net/fopen:
*Windows* offers a text-mode translation flag ('t') ...
Small correction of the original bug report: \n is always used, have a look at
ext/standard/file.c:~1991:
(...)
smart_str_appendc(&csvline, '\n');
smart_str_0(&csvline);
(...)
------------------------------------------------------------------------
[2007-08-21 06:20:27] derick@php.net
Does it work if you open the file in text mode ("wt") ? That *should* do the trick.
------------------------------------------------------------------------
[2007-08-21 06:13:14] php at phihag dot de
Description:
------------
Currently, there is no way to set a custom line ending when writing CSV files with fputcsv. For
instance, this is needed when writing CSV for various Microsoft products which require
"\r\n" as line ending.
Therefore, an optional parameter $ending (as for stream_get_line), which defaults to auto-detection
(current behaviour), would be nice.
To ensure the result is still a valid CSV file, either document using anything else than line
endings (or tabs etc.) is not a good idea, or define/use constants for the common line endings.
Reproduce code:
---------------
<?php
$f = fopen("php://stdout", "w");
fputcsv($f, array("a", "b c"), ",", '"',
"\r\n");
fclose($f);
?>
Expected result:
----------------
As a php string:
"a,\"b c\"\r\n"
Actual result:
--------------
n/a, without the last parameter: "a,\"b c\"\n" (hardcoded line ending in file.c)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=42357&edit=1