From 2e0e9b239894665cebb32a1e8858b864020bf45f Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 17 Mar 2016 23:05:14 +0000 Subject: [PATCH] Detect the presence of a sep=; line in CSV imports, and set the delimiter accordingly --- src/PhpSpreadsheet/Reader/CSV.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/PhpSpreadsheet/Reader/CSV.php b/src/PhpSpreadsheet/Reader/CSV.php index 937feee0..a6e0d0be 100644 --- a/src/PhpSpreadsheet/Reader/CSV.php +++ b/src/PhpSpreadsheet/Reader/CSV.php @@ -150,6 +150,23 @@ class CSV extends BaseReader implements IReader } } + /** + * Identify any separator that is explicitly set in the file + * + */ + protected function checkSeparator() + { + $line = fgets($this->fileHandle); + if ($line === false) { + return; + } + + if ((strlen(trim($line)) == 5) && (strpos($line, 'sep=') !== 0)) { + return $this->skipBOM(); + } + $this->delimiter = substr($line, 4, 1); + } + /** * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns) * @@ -168,6 +185,7 @@ class CSV extends BaseReader implements IReader // Skip BOM, if any $this->skipBOM(); + $this->checkSeparator(); $escapeEnclosures = array( "\\" . $this->enclosure, $this->enclosure . $this->enclosure ); @@ -232,6 +250,7 @@ class CSV extends BaseReader implements IReader // Skip BOM, if any $this->skipBOM(); + $this->checkSeparator(); // Create new PHPExcel object while ($objPHPExcel->getSheetCount() <= $this->sheetIndex) {