diff --git a/src/Spout/Common/Helper/GlobalFunctionsHelper.php b/src/Spout/Common/Helper/GlobalFunctionsHelper.php index 608854a..0b5f6f1 100644 --- a/src/Spout/Common/Helper/GlobalFunctionsHelper.php +++ b/src/Spout/Common/Helper/GlobalFunctionsHelper.php @@ -86,7 +86,13 @@ class GlobalFunctionsHelper */ public function fgetcsv($handle, $length = null, $delimiter = null, $enclosure = null) { - return fgetcsv($handle, $length, $delimiter, $enclosure); + // PHP uses '\' as the default escape character. This is not RFC-4180 compliant... + // To fix that, simply disable the escape character. + // @see https://bugs.php.net/bug.php?id=43225 + // @see http://tools.ietf.org/html/rfc4180 + $escapeCharacter = "\0"; + + return fgetcsv($handle, $length, $delimiter, $enclosure, $escapeCharacter); } /** @@ -101,7 +107,13 @@ class GlobalFunctionsHelper */ public function fputcsv($handle, array $fields, $delimiter = null, $enclosure = null) { - return fputcsv($handle, $fields, $delimiter, $enclosure); + // PHP uses '\' as the default escape character. This is not RFC-4180 compliant... + // To fix that, simply disable the escape character. + // @see https://bugs.php.net/bug.php?id=43225 + // @see http://tools.ietf.org/html/rfc4180 + $escapeCharacter = "\0"; + + return fputcsv($handle, $fields, $delimiter, $enclosure, $escapeCharacter); } /** diff --git a/tests/Spout/Reader/CSV/ReaderTest.php b/tests/Spout/Reader/CSV/ReaderTest.php index 6103ba6..da53cf3 100644 --- a/tests/Spout/Reader/CSV/ReaderTest.php +++ b/tests/Spout/Reader/CSV/ReaderTest.php @@ -219,12 +219,23 @@ class ReaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals('This is, a comma', $allRows[0][0]); } + /** + * @return void + */ + public function testReadShouldSupportEscapedCharacters() + { + $allRows = $this->getAllRowsForFile('csv_with_escaped_characters.csv'); + + $expectedRow = ['"csv--11"', 'csv--12\\', 'csv--13\\\\', 'csv--14\\\\\\']; + $this->assertEquals([$expectedRow], $allRows); + } + /** * @return void */ public function testReadShouldNotTruncateLineBreak() { - $allRows = $this->getAllRowsForFile('csv_with_line_breaks.csv', ','); + $allRows = $this->getAllRowsForFile('csv_with_line_breaks.csv'); $this->assertEquals("This is,\na comma", $allRows[0][0]); } diff --git a/tests/Spout/Writer/CSV/WriterTest.php b/tests/Spout/Writer/CSV/WriterTest.php index f7e4a6e..63a7d7c 100644 --- a/tests/Spout/Writer/CSV/WriterTest.php +++ b/tests/Spout/Writer/CSV/WriterTest.php @@ -175,6 +175,17 @@ class WriterTest extends \PHPUnit_Framework_TestCase $this->assertEquals('#This is, a comma#,csv--12,csv--13', $writtenContent, 'The fields should be enclosed with #'); } + public function testWriteShouldSupportedEscapedCharacters() + { + $allRows = $this->createRowsFromValues([ + ['"csv--11"', 'csv--12\\', 'csv--13\\\\', 'csv--14\\\\\\'], + ]); + $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_with_escaped_characters.csv'); + $writtenContent = $this->trimWrittenContent($writtenContent); + + $this->assertEquals('"""csv--11""",csv--12\\,csv--13\\\\,csv--14\\\\\\', $writtenContent, 'The \'"\' and \'\\\' characters should be properly escaped'); + } + /** * @param Row[] $allRows * @param string $fileName diff --git a/tests/resources/csv/csv_with_escaped_characters.csv b/tests/resources/csv/csv_with_escaped_characters.csv new file mode 100644 index 0000000..ee698c5 --- /dev/null +++ b/tests/resources/csv/csv_with_escaped_characters.csv @@ -0,0 +1 @@ +"""csv--11""","csv--12\","csv--13\\","csv--14\\\"