|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-06-12 01:27 UTC] danack@php.net
[2017-06-12 02:45 UTC] andreas at dqxtech dot net
[2017-06-12 02:49 UTC] andreas at dqxtech dot net
[2017-06-12 02:50 UTC] andreas at dqxtech dot net
[2017-06-12 07:06 UTC] spam2 at rhsoft dot net
[2017-09-21 11:27 UTC] cmb@php.net
[2017-09-21 16:09 UTC] andreas at dqxtech dot net
[2017-09-21 16:48 UTC] cmb@php.net
[2018-09-10 16:17 UTC] theodorejb at outlook dot com
[2018-09-12 22:53 UTC] cmb@php.net
-Status: Open
+Status: Duplicate
-Assigned To:
+Assigned To: cmb
[2018-09-12 22:53 UTC] cmb@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 10:00:01 2025 UTC |
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"""', )