Re: fputcsv() and $escape character
| From: | Andreas Hennings | Date: | Thu, 21 Sep 2017 17:32:57 +0000 |
| Subject: | Re: fputcsv() and $escape character | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-100737@lists.php.net to get a copy of this message | ||
So empty string would enable the standard behavior RFC 7111 with no escape char.
If so, I support this.
I don't know if '' or true / false / null should be this "special value".
It has to be something that was not legal before, and that people
should use intentionally and not by accident.
I guess '' is good enough for this, or not worse than other options.
One question: Does this also require a change in fgetcsv()?
So it can read csv without escape character?
I remember that fgetcsv() does currently tolerate some broken CSV. It
should continue to do so for BC reasons.
For the record, here are some links:
https://stackoverflow.com/questions/44427926/data-gets-garbled-when-writing-to-csv-with-fputcsv-fgetcsv/46342634#46342634
https://bugs.php.net/bug.php?id=74713&edit=2
On Thu, Sep 21, 2017 at 1:43 PM, Christoph M. Becker <cmbecker69@gmx.de> wrote:
> Hi everybody!
>
> There are several bug reports regarding "broken" fputcsv() behavior in
> our tracker, namely, because the $escape parameter causes unexpected
> results. For instance:
>
> <?php
> $row = ['a\\"', 'bbb'];
>
> $fp = fopen('php://memory', 'w+');
> fputcsv($fp, $row);
> rewind($fp);
> echo stream_get_contents($fp);
> fclose($fp);
> ?>
>
> outputs
>
> "a\"",bbb
>
> instead of the expected
>
> "a\""",bbb
>
> I don't think the current behavior is a bug, but rather the escape
> character is an extension to the CSV "standard" (RFC 7111). One can
> practically disable the escape character by passing any character that
> is not contained in any of the strings in the array; "\0" is usually a
> good choice, so changing line 5 in the script above to the following
> gives the desired behavior:
>
> fputcsv($fp, $row, ',', '"', "\0");
>
> Cf. <https://3v4l.org/InlUv> vs.
> <https://3v4l.org/tVFBo>.
>
> It is, however, not possible to pass an empty string as $escape
> argument, because fputcsv() bails out in this case raising
>
> Warning: fputcsv(): escape must be a character
>
> I suggest to allow an empty string instead, and to consider making this
> the default in a future version, probably after some time of deprecating
> any other $escape argument.
>
> Thoughts?
>
> --
> Christoph M. Becker
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>