#31074 [Asn->Bgs]: Wrong behavior in fgetcsv if field contains newlines AND \ followed by "

From: Date: Tue, 18 Jan 2005 00:13:30 +0000
Subject: #31074 [Asn->Bgs]: Wrong behavior in fgetcsv if field contains newlines AND \ followed by "
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-72342@lists.php.net to get a copy of this message
ID: 31074 Updated by: iliaa@php.net Reported By: b_ulrich at t-online dot de -Status: Assigned +Status: Bogus Bug Type: Filesystem function related Operating System: Linux PHP Version: 4.3.10 Assigned To: iliaa New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php In un-quoted strings a new line breaks the line. Previous Comments: ------------------------------------------------------------------------ [2004-12-13 21:30:23] b_ulrich at t-online dot de Description: ------------ If a csv field contains something like ...\"...\"... followed by a newline, fgetcsv assumes that the newline is the end of the datarow even it isn't the end of the field. There's no problem if the field only contains text and newlines or only ...\"...\"... If you create a Spreadsheet (2Rows with 3Columns) containing this: <Row 1 Column 1>: R1C1 <Row 1 Column 2>: say \"YES\" to element in next line <Row 1 Column 3>: R1C3 <Row 2 Column 1>: R2C1 <Row 2 Column 2>: something with multilines <Row 2 Column 3>: R2C3 and export this to CSV, you will get: R1C1;"say \""YES\"" to element in next line";R1C3 R2C1;"something with multilines";R2C3 This is correct Standard-CSV. But if you try to read it with fgetcsv you will get a Result containing 3 Rows, the first and the second containing 2 Elements, the third contains 3 Elements. Reproduce code: --------------- <?php $csv_data="R1C1;\"say \\\"\"YES\\\"\" to\nelement in next line\";R1C3\nR2C1;\"something with\nmultilines\";R2C3\n"; $filename = "/tmp/test.csv"; $handle = fopen($filename, "wb"); fwrite($handle, $csv_data,strlen($csv_data)); fclose($handle); $handle = fopen($filename, "rb"); while (($data = fgetcsv($handle,100000,";","\"")) !== FALSE) { $result[]=$data; } // fclose($handle); print_r($result); ?> Expected result: ---------------- Array ( [0] => Array ( [0] => R1C1 [1] => say \"YES\" to element in next line [2] => R1C3 ) [1] => Array ( [0] => R2C1 [1] => something with multilines [2] => R2C3 ) ) Actual result: -------------- Array ( [0] => Array ( [0] => R1C1 [1] => say \"YES\" to ) [1] => Array ( [0] => element in next line" [1] => R1C3 ) [2] => Array ( [0] => R2C1 [1] => something with multilines [2] => R2C3 ) ) Note: There's no Problem, if there is no linebreak in Row1 Column2 and there is also no Problem if you don't have the backslashes in the line. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=31074&edit=1

« previous php.bugs (#72342) next »