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:
commit
d87cdd84b3
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user