Bug #74713 [NEW]: CSV cell split after fputcsv() + fgetcsv() round trip.

From: Date: Thu, 08 Jun 2017 17:14:16 +0000
Subject: Bug #74713 [NEW]: CSV cell split after fputcsv() + fgetcsv() round trip.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209422@lists.php.net to get a copy of this message
From: andreas at dqxtech dot net Operating system: Linux / 3v4l PHP version: 7.1.5 Package: Filesystem function related Bug Type: Bug Bug description:CSV cell split after fputcsv() + fgetcsv() round trip. Description: ------------ If one cell of the data sent to fputcsv() contains "{$enclosure}{$escape_char}{$escape_char}{$enclosure}{$delimiter}", this cell will be split after a round trip of fputcsv() + fgetcsv(). E.g. with specific choice of delimiter, enclosure and escape character: Send ['"@@","B"'] to fputcsv() as a row of data. fgetcsv() gives you back ['"@@', 'B"""']. https://3v4l.org/3ZUO8 The reason is that fputcsv() writes '"""@@",""B"""' to the file, instead of '"""@@"",""B"""'. In fact, writing the latter explicitly with fwrite() instead of fputcsv() fixes the problem: https://3v4l.org/mjQRi See https://stackoverflow.com/questions/44427926/data-gets-garbled-when-writing-to-csv-with-fputcsv-fgetcsv Especially this answer, https://stackoverflow.com/a/44441433/246724 Test script: --------------- $delimiter = ','; $enclosure = '"'; $escape_char = "@"; $row_before = ["{$enclosure}{$escape_char}{$escape_char}{$enclosure}{$delimiter}{$enclosure}B{$enclosure}"]; print "\nBEFORE:\n"; var_export($row_before); print "\n"; $fh = fopen($file = 'php://temp', 'rb+'); fputcsv($fh,$row_before,$delimiter,$enclosure, $escape_char); # fwrite($fh, '"""@@"",""B"""'); rewind($fh); $row_plain = fread($fh, 1000); print "\nPLAIN:\n"; var_export($row_plain); print "\n"; rewind($fh); $row_after = fgetcsv($fh, 500,$delimiter,$enclosure, $escape_char); print "\nAFTER:\n"; var_export($row_after); print "\n\n"; fclose($fh); Expected result: ---------------- BEFORE: array ( 0 => '"@@","B"', ) PLAIN: '"""@@"",""B""" ' AFTER: array ( 0 => '"@@","B"', ) Actual result: -------------- BEFORE: array ( 0 => '"@@","B"', ) PLAIN: '"""@@",""B""" ' AFTER: array ( 0 => '"@@', 1 => 'B"""', ) -- Edit bug report at https://bugs.php.net/bug.php?id=74713&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74713&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74713&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74713&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=74713&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=74713&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=74713&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=74713&r=needscript Try newer version: https://bugs.php.net/fix.php?id=74713&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=74713&r=support Expected behavior: https://bugs.php.net/fix.php?id=74713&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=74713&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=74713&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=74713&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74713&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=74713&r=dst IIS Stability: https://bugs.php.net/fix.php?id=74713&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=74713&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=74713&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=74713&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=74713&r=mysqlcfg

« previous php.bugs (#209422) next »