Skip to content
This repository has been archived by the owner on Jan 2, 2019. It is now read-only.

Commit

Permalink
Detect the presence of a sep=; line in CSV imports, and set the delim…
Browse files Browse the repository at this point in the history
…iter accordingly
  • Loading branch information
MarkBaker committed Mar 17, 2016
1 parent a806b79 commit 79f9521
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Classes/PHPExcel/Reader/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ protected function skipBOM()
}
}

/**
* 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)
*
Expand All @@ -175,6 +192,7 @@ public function listWorksheetInfo($pFilename)

// Skip BOM, if any
$this->skipBOM();
$this->checkSeparator();

$escapeEnclosures = array( "\\" . $this->enclosure, $this->enclosure . $this->enclosure );

Expand Down Expand Up @@ -239,6 +257,7 @@ public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)

// Skip BOM, if any
$this->skipBOM();
$this->checkSeparator();

// Create new PHPExcel object
while ($objPHPExcel->getSheetCount() <= $this->sheetIndex) {
Expand Down

5 comments on commit 79f9521

@SpikedCola
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->delimiter = substr($line, 4, 1);

Why is the delimiter assumed to be the 5th character?

@MarkBaker
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because

sep=x
01234

That's the standard when a sep= is used

What character would you expect it to be?

@SpikedCola
Copy link

@SpikedCola SpikedCola commented on 79f9521 Apr 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the logic in the case where sep= is defined, but in the case where sep= isn't present, this seems to just pick the 5th character in the first row of data. I believe there should be a check for "sep=", something like:

if (stripos($line, 'sep=') === 0) {
   $this->delimiter = substr($line, 4, 1);
}

I had to add this check in order to get a "plain old" CSV file to parse (or maybe I'm just crazy)

@MarkBaker
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and that's been raised as an issue

I'm testing changes to it at the moment

@SpikedCola
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, my apologies! I didn't put enough effort into searching issues

Please sign in to comment.