Allow _guess_delimiter to work with a single row of data

Fix #206
This commit is contained in:
Fonata
2021-10-16 18:28:19 +02:00
committed by Fonata
parent d5606f8b2a
commit 5d4643b201
3 changed files with 11 additions and 1 deletions

View File

@@ -1261,7 +1261,7 @@ class Csv {
}
}
if ($equal) {
if ($equal || $depth === 1) {
$match = $almost ? 2 : 1;
$pref = strpos($preferred, $char);
$pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999';

View File

@@ -0,0 +1 @@
C1,C2,C3
1 C1 C2 C3

View File

@@ -121,6 +121,15 @@ class ParseTest extends TestCase {
self::assertEquals($expected, $this->csv->data);
}
public function testSingleRow() {
$this->csv->auto(__DIR__ . '/../example_files/single_row.csv');
self::assertEquals([], $this->csv->data, 'Single row is detected as header');
$this->csv->heading = false;
$this->csv->auto(__DIR__ . '/../example_files/single_row.csv');
$expected = [['C1', 'C2', 'C3']];
self::assertEquals($expected, $this->csv->data);
}
public function testMatomoData() {
// Matomo (Piwik) export cannot be read with
$this->csv->use_mb_convert_encoding = true;