Commit 7be6438a authored by Tobias Zeuch's avatar Tobias Zeuch
Browse files

Make CSV-Import autodetect delimiter

Bug 1393472: Automatically detect the delimiter used to store the fields in the
csv field. The problem this should solve is that Microsoft Excel uses a
semicolon instead of a comma if the language setting of Windows is set to
The first line of the csv-file contains the header fields so we use that line
to check, if it contains a comma or a semicolon since none of these appears in
the header field names itself.

Change-Id: I833041eb2169fc5ccc9557e0debf3f03c8daf7cc
Signed-off-by: default avatarTobias Zeuch <>
parent aeda6261
......@@ -89,8 +89,9 @@ class CsvFile {
return; // file is not open
$delimiter = $this->detectDelimiter();
$i = 0;
while (($line = fgetcsv($this->filehandle, MAX_LINE_LENGTH)) !== false) {
while (($line = fgetcsv($this->filehandle, MAX_LINE_LENGTH, $delimiter)) !== false) {
// Get the format of the file
if ($this->headerExists && $i == 1) {
......@@ -140,6 +141,36 @@ class CsvFile {
* detect the delimiter using the first line that should consist only of
* the header fields, which strictly consist of the characters [a-zA-Z0-9_]
* so the known delimiters (so far comma and semicolon) don't appear in those
* fields. <br/>
* Background is that Microsoft separates the fields in csv-files with
* semicolons when the System language is set to German
* @return string the delimiter used to separate the fields in the file
private function detectDelimiter() {
static $knowndelimiters = array(
' '
$firstline = fgets($this->filehandle);
fseek($this->filehandle, 0);
foreach ($knowndelimiters as $delimiter) {
if (strpos($firstline, $delimiter) > 0) {
return $delimiter;
// Default: the comma. In case we have a file with only one field per
// line, we cannot detect the delimiter. Luckily Mahara always expects
// more than one mandatory fields, so getting here usually means the
// file cannot be imported anyway
return ',';
class CSVErrors {
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment