From b01671213a14620220a7240d55d2b31882580b71 Mon Sep 17 00:00:00 2001 From: Kurounin Date: Mon, 3 Apr 2017 11:52:35 +0900 Subject: [PATCH] Removed double un-escaping when reading CSV Removed "unescape enclosure functionality", since the unescaping is already handled by fgetcsv, and performing the unescaping again would actually result int the text from the cell being read wrong. As an example try parsing the folowing CSV: ``` "" ``` With the additional unescaping it would have ended up as: ```  src= ``` instead of the correct: ``` ``` Fixes https://github.com/PHPOffice/PHPExcel/pull/1171 --- src/PhpSpreadsheet/Reader/Csv.php | 10 -------- tests/PhpSpreadsheetTests/Reader/CsvTest.php | 27 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Reader/CsvTest.php diff --git a/src/PhpSpreadsheet/Reader/Csv.php b/src/PhpSpreadsheet/Reader/Csv.php index f36366e3..5c623fa5 100644 --- a/src/PhpSpreadsheet/Reader/Csv.php +++ b/src/PhpSpreadsheet/Reader/Csv.php @@ -172,8 +172,6 @@ class Csv extends BaseReader implements IReader $this->skipBOM(); $this->checkSeparator(); - $escapeEnclosures = ['\\' . $this->enclosure, $this->enclosure . $this->enclosure]; - $worksheetInfo = []; $worksheetInfo[0]['worksheetName'] = 'Worksheet'; $worksheetInfo[0]['lastColumnLetter'] = 'A'; @@ -246,11 +244,6 @@ class Csv extends BaseReader implements IReader } $sheet = $spreadsheet->setActiveSheetIndex($this->sheetIndex); - $escapeEnclosures = [ - '\\' . $this->enclosure, - $this->enclosure . $this->enclosure, - ]; - // Set our starting row based on whether we're in contiguous mode or not $currentRow = 1; if ($this->contiguous) { @@ -262,9 +255,6 @@ class Csv extends BaseReader implements IReader $columnLetter = 'A'; foreach ($rowData as $rowDatum) { if ($rowDatum != '' && $this->readFilter->readCell($columnLetter, $currentRow)) { - // Unescape enclosures - $rowDatum = str_replace($escapeEnclosures, $this->enclosure, $rowDatum); - // Convert encoding if necessary if ($this->inputEncoding !== 'UTF-8') { $rowDatum = \PhpOffice\PhpSpreadsheet\Shared\StringHelper::convertEncoding($rowDatum, 'UTF-8', $this->inputEncoding); diff --git a/tests/PhpSpreadsheetTests/Reader/CsvTest.php b/tests/PhpSpreadsheetTests/Reader/CsvTest.php new file mode 100644 index 00000000..ee092eb2 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/CsvTest.php @@ -0,0 +1,27 @@ +'; + $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet'); + + // Write temp file with value + $spreadsheet = new Spreadsheet(); + $spreadsheet->getActiveSheet()->getCell('A1')->setValue($value); + $writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet); + $writer->save($filename); + + // Read written file + $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv(); + $reloadedSpreadsheet = $reader->load($filename); + $actual = $reloadedSpreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue(); + $this->assertSame($value, $actual, 'should be able to write and read strings with multiples quotes'); + } +}