php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75418 fputcsv() escape parameter meaningless
Submitted: 2017-10-23 13:59 UTC Modified: 2018-02-15 13:22 UTC
From: phofstetter at sensational dot ch Assigned: cmb (profile)
Status: Duplicate Package: Filesystem function related
PHP Version: 7.2.0RC4 OS: macOS, Linux
Private report: No CVE-ID: None
 [2017-10-23 13:59 UTC] phofstetter at sensational dot ch
Description:
------------
Since 5.4.4 there's  an `escape_char` parameter that's defaulting to \. 

It looks like that parameter is meaningless though as of all the possible values passed in, only " really does something and what it does is break the CSV encoding.

Test script:
---------------
<?php

$fh = fopen('php://stdout', 'w');
fputcsv($fh, ['using default', 'a,"b'], ',', '"');
fputcsv($fh, ['using "      ', 'a,"b'], ',', '"', '"');
fputcsv($fh, ['using x      ', 'a,"b'], ',', '"', 'x');
fputcsv($fh, ['using \\      ', 'a,"b'], ',', '"', '\\');

Expected result:
----------------
"using default","a,\"b"
"using "      ","a,""b"
"using x      ","a,x"b"
"using \      ","a,\"b"

Actual result:
--------------
"using default","a,""b"
"using "      ","a,"b"
"using x      ","a,""b"
"using \      ","a,""b"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-24 17:32 UTC] cmb@php.net
> It looks like that parameter is meaningless though as of all the
> possible values passed in, only " really does something […]

That's not quite right, see
<https://externals.io/message/100729#100729>, for instance.

> […] and what it does is break the CSV encoding.

Indeed, using the $escape parameter with fputcsv() can easily
produce non-standard CSV (if there were such a standard).
Frankly, I do not know which use-case has been targetted by
introducing the $escape parameter.
 [2018-02-15 13:22 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2018-02-15 13:22 UTC] cmb@php.net
Closing as duplicate of bug #43225.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 05:01:29 2024 UTC