From 4488dff047ec3d9bd189bd0f0b97f568faa28350 Mon Sep 17 00:00:00 2001 From: Nassim Jammal Date: Tue, 21 Jun 2016 11:00:07 -0400 Subject: [PATCH] fix issues with new lines starting with ", multiline with no text https://github.com/kzykhys/PHPCsvParser/issues/8 and newline after quote https://github.com/kzykhys/PHPCsvParser/issues/7 --- .gitignore | 3 ++- .../CsvParser/Iterator/CsvIterator.php | 20 ++++++++++++------- test/KzykHys/CsvParser/CsvParserTest.php | 4 ++-- .../Resources/csv/7-multiline-3.CR.csv | 1 + .../Resources/csv/7-multiline-3.CRLF.csv | 3 +++ .../Resources/csv/7-multiline-3.LF.csv | 3 +++ .../Resources/csv/7-multiline-3.json | 3 +++ 7 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 test/KzykHys/CsvParser/Resources/csv/7-multiline-3.CR.csv create mode 100644 test/KzykHys/CsvParser/Resources/csv/7-multiline-3.CRLF.csv create mode 100644 test/KzykHys/CsvParser/Resources/csv/7-multiline-3.LF.csv create mode 100644 test/KzykHys/CsvParser/Resources/csv/7-multiline-3.json diff --git a/.gitignore b/.gitignore index 5db6644..6000e67 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor/ *.phar composer.lock -phpunit.xml \ No newline at end of file +phpunit.xml +*~ diff --git a/src/KzykHys/CsvParser/Iterator/CsvIterator.php b/src/KzykHys/CsvParser/Iterator/CsvIterator.php index 72e0e2e..f1050f2 100644 --- a/src/KzykHys/CsvParser/Iterator/CsvIterator.php +++ b/src/KzykHys/CsvParser/Iterator/CsvIterator.php @@ -85,19 +85,25 @@ public function current() // loop over the columns foreach ($tokens as $value) { $value = preg_replace('/"(\r\n|\r|\n)*$/', '"', $value); + // check the first letter is 'enclosure' or not if (substr($value, 0, 1) == $this->option['enclosure']) { // check the last letter is 'enclosure' - if (substr($value, -1) == $this->option['enclosure']) { - $this->processEnclosedField($value, $this->option); - } else { - $this->processContinuousField($value, $this->option); + if ($this->continue) { + $this->processClosingField($value, $this->option); + } + else { + if (strlen($value) > 1 && (substr($value, -1) == $this->option['enclosure'])) { + $this->processEnclosedField($value, $this->option); + } else { + $this->processContinuousField($value, $this->option); + } } } else { // first letter is NOT 'enclosure' // check the last letter is 'enclosure' - if(substr($value, -1) == $this->option['enclosure']) { + if (substr($value, -1) == $this->option['enclosure']) { $this->processClosingField($value, $this->option); } else { $this->processField($value, $this->option); @@ -118,7 +124,7 @@ public function current() $this->revert = ""; $this->iterator->next(); } - + return $this->result; } @@ -344,4 +350,4 @@ public function rewind() $this->key = 0; } -} \ No newline at end of file +} diff --git a/test/KzykHys/CsvParser/CsvParserTest.php b/test/KzykHys/CsvParser/CsvParserTest.php index d11c7a7..aff3c11 100755 --- a/test/KzykHys/CsvParser/CsvParserTest.php +++ b/test/KzykHys/CsvParser/CsvParserTest.php @@ -10,7 +10,7 @@ public function providePatterns() $dir = __DIR__ . '/Resources/csv/'; $names = array( - '1-plain', '3-quote-escaping', '4-multiline', '5-multiline-2' + '1-plain', '3-quote-escaping', '4-multiline', '5-multiline-2', '7-multiline-3' ); $patterns = array(); @@ -109,4 +109,4 @@ public function testMultibyteString() } -} \ No newline at end of file +} diff --git a/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.CR.csv b/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.CR.csv new file mode 100644 index 0000000..774007d --- /dev/null +++ b/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.CR.csv @@ -0,0 +1 @@ +", Column 1","Column 2 ","Column 3" \ No newline at end of file diff --git a/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.CRLF.csv b/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.CRLF.csv new file mode 100644 index 0000000..1ba01ef --- /dev/null +++ b/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.CRLF.csv @@ -0,0 +1,3 @@ +", +Column 1","Column 2 +","Column 3" diff --git a/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.LF.csv b/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.LF.csv new file mode 100644 index 0000000..1ba01ef --- /dev/null +++ b/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.LF.csv @@ -0,0 +1,3 @@ +", +Column 1","Column 2 +","Column 3" diff --git a/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.json b/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.json new file mode 100644 index 0000000..bd6fb9d --- /dev/null +++ b/test/KzykHys/CsvParser/Resources/csv/7-multiline-3.json @@ -0,0 +1,3 @@ +[ + [",\nColumn 1", "Column 2\n", "Column 3"] +]