Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer CSV delimiter if it hasn't been set explicitly #141

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion src/PhpSpreadsheet/Reader/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Csv extends BaseReader implements IReader
*
* @var string
*/
private $delimiter = ',';
private $delimiter = null;

/**
* Enclosure.
Expand Down Expand Up @@ -152,6 +152,86 @@ protected function checkSeparator()
return $this->skipBOM();
}

/**
* Infer the separator if it isn't explicitly set in the file or specified by the user.
*/
protected function inferSeparator()
{
if ($this->delimiter !== null) {
return;
}

$potentialDelimiters = [',', ';', "\t", '|', ':', ' '];
$counts = [];
foreach ($potentialDelimiters as $delimiter) {
$counts[$delimiter] = [];
}

// Count how many times each of the potential delimiters appears in each line
$numberLines = 0;
while (($line = fgets($this->fileHandle)) !== false && (++$numberLines < 1000)) {
$countLine = [];
for ($i = strlen($line) - 1; $i >= 0; --$i) {
$char = $line[$i];
if (isset($counts[$char])) {
if (!isset($countLine[$char])) {
$countLine[$char] = 0;
}
++$countLine[$char];
}
}
foreach ($potentialDelimiters as $delimiter) {
$counts[$delimiter][] = isset($countLine[$delimiter])
? $countLine[$delimiter]
: 0;
}
}

// Calculate the mean square deviations for each delimiter (ignoring delimiters that haven't been found consistently)
$meanSquareDeviations = [];
$middleIdx = floor(($numberLines - 1) / 2);

foreach ($potentialDelimiters as $delimiter) {
$series = $counts[$delimiter];
sort($series);

$median = ($numberLines % 2)
? $series[$middleIdx]
: ($series[$middleIdx] + $series[$middleIdx + 1]) / 2;

if ($median === 0) {
continue;
}

$meanSquareDeviations[$delimiter] = array_reduce(
$series,
function ($sum, $value) use ($median) {
return $sum + pow($value - $median, 2);
}
) / count($series);
}

// ... and pick the delimiter with the smallest mean square deviation (in case of ties, the order in potentialDelimiters is respected)
$min = INF;
foreach ($potentialDelimiters as $delimiter) {
if (!isset($meanSquareDeviations[$delimiter])) {
continue;
}

if ($meanSquareDeviations[$delimiter] < $min) {
$min = $meanSquareDeviations[$delimiter];
$this->delimiter = $delimiter;
}
}

// If no delimiter could be detected, fall back to the default
if ($this->delimiter === null) {
$this->delimiter = reset($potentialDelimiters);
}

return $this->skipBOM();
}

/**
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
*
Expand All @@ -171,6 +251,7 @@ public function listWorksheetInfo($pFilename)
// Skip BOM, if any
$this->skipBOM();
$this->checkSeparator();
$this->inferSeparator();

$worksheetInfo = [];
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
Expand Down Expand Up @@ -237,6 +318,7 @@ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
// Skip BOM, if any
$this->skipBOM();
$this->checkSeparator();
$this->inferSeparator();

// Create new PhpSpreadsheet object
while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
Expand Down
14 changes: 14 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,18 @@ public function testEnclosure()
$actual = $reloadedSpreadsheet->getActiveSheet()->getCell('A1')->getCalculatedValue();
$this->assertSame($value, $actual, 'should be able to write and read strings with multiples quotes');
}

public function testDelimiterDetection()
{
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$this->assertNull($reader->getDelimiter());

$filename = __DIR__ . '/../../data/Reader/CSV/semicolon_separated.csv';
$spreadsheet = $reader->load($filename);

$this->assertSame(';', $reader->getDelimiter(), 'should be able to infer the delimiter');

$actual = $spreadsheet->getActiveSheet()->getCell('C2')->getValue();
$this->assertSame('25,5', $actual, 'should be able to retrieve values with commas');
}
}
3 changes: 3 additions & 0 deletions tests/data/Reader/CSV/semicolon_separated.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This;Are;Headers
Cell A2;Number with comma;25,5
Two colons and a comma;B|3;:,: