Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.0.0

Bug #14030 File_CSV::write() does not correctly quote field values
Submitted: 2008-05-30 12:37 UTC
From: walter Assigned: dufuz
Status: Closed Package: File_CSV
PHP Version: 5.2.6 OS: FreeBSD
Roadmaps: 1.0.0    

 [2008-05-30 12:37 UTC] walter (Walter Hop)
Description: ------------ There are a few problems with quoting of field values in File_CSV::write(). A CSV field MUST be "quoted" in the following cases: a. the CSV separator appears in the field value (this is being checked, although the check has a bug) b. the quote character appears in the field value (this is currently not checked) c. the newline character appears in the field value (this is currently not checked) d. the field value starts or ends with a space (this is currently not checked) Second, strpos($fields[$i], $conf['sep']) in line 471 to check for the presence of a separator within the field is incorrect. The check should read: strpos($fields[$i], $conf['sep']) !== false. Third, when a field MUST be quoted, NOT using quotes violates the integrity of the written CSV. Therefore, a null value of $conf['quote'] should really be disallowed. However, to not confront people with weird run-time errors, I found it better to default to the doublequote as a quote char. Test script: --------------- <?php require "File/CSV.php"; //PEAR $rows = array( array(1, "I may be quoted", 1), array(2, "I \"must\" \"be\" quoted", 2), array(3, " I must be quoted", 3), array(4, "I must be quoted ", 4), array(5, "I must; \"be\"; quoted", 5), array(6, "I must\nbe\nquoted", 6), ); $csv_conf = array("fields" => 3, "crlf" => "\r\n", "quote" => "\"", "sep" => ";"); $csv_temp_filename = "csvtest.csv"; foreach ($rows as $csv_row) { $res = File_CSV::write($csv_temp_filename, $csv_row, $csv_conf); } Expected result: ---------------- 1;I may be quoted;1 2;"I ""must"" ""be"" quoted";2 3;" I must be quoted";3 4;"I must be quoted ";4 5;"I must; ""be""; quoted";5 6;"I must be quoted";6 Actual result: -------------- 1;I may be quoted;1 2;I "must" "be" quoted;2 3; I must be quoted;3 4;I must be quoted ;4 5;"I must; "be"; quoted";5 6;I must be quoted;6


 [2008-06-25 21:45 UTC] dufuz (Helgi Þormar Þorbjörnsson)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on by the end of next Sunday (CET). If this was a problem with the website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.