1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #8009 from checkitsedo/#8008-CSV-Import-AutoDetect-Delimiter

#8008 csv import auto detect delimiter
This commit is contained in:
David Bomba 2022-11-30 16:03:06 +11:00 committed by GitHub
commit d87cdd84b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -136,6 +136,8 @@ class ImportController extends Controller
}
$csv = Reader::createFromString($csvfile);
$csvdelimiter = self::detectDelimiter($csvfile);
$csv->setDelimiter($csvdelimiter);
$stmt = new Statement();
$data = iterator_to_array($stmt->process($csv));
@ -156,4 +158,17 @@ class ImportController extends Controller
return $data;
}
public function detectDelimiter($csvfile)
{
$delimiters = array(',', '.', ';');
$bestDelimiter = false;
$count = 0;
foreach ($delimiters as $delimiter)
if (substr_count($csvfile, $delimiter) > $count) {
$count = substr_count($csvfile, $delimiter);
$bestDelimiter = $delimiter;
}
return $bestDelimiter;
}
}