Allow use of alternate delimiters in csv files

This commit is contained in:
fiedorow 2004-04-14 20:46:39 +00:00
parent eb3daafe3f
commit 811cf8919d

View File

@ -30,6 +30,17 @@
$struploadusers = get_string("uploadusers");
$straddnewuser = get_string("importuser");
$csv_encode = '/\&\#44/';
if (isset($CFG->CSV_DELIMITER)) {
$csv_delimiter = "\" . $CFG->CSV_DELIMITER;
$csv_delimiter2 = $CFG->CSV_DELIMITER;
if (isset($CFG->CSV_ENCODE)) {
$csv_encode = '/\&\#' . $CFG->CSV_ENCODE . '/';
}
} else {
$csv_delimiter = "\,";
$csv_delimiter2 = ",";
}
/// Print the header
@ -88,7 +99,7 @@
"group5" =>1);
// --- get header (field names) ---
$header = split("\,", fgets($fp,1024));
$header = split($csv_delimiter, fgets($fp,1024));
// check for valid field names
foreach ($header as $i => $h) {
$h = trim($h); $header[$i] = $h; // remove whitespace
@ -111,11 +122,12 @@
foreach ($optionalDefaults as $key => $value) {
$user->$key = addslashes($adminuser->$key);
}
//Note: commas within a field should be encoded as &#44
$line = split("\,", fgets($fp,1024));
//Note: commas within a field should be encoded as &#44 (for comma separated csv files)
//Note: semicolon within a field should be encoded as &#59 (for semicolon separated csv files)
$line = split($csv_delimiter, fgets($fp,1024));
foreach ($line as $key => $value) {
//decode encoded commas
$record[$header[$key]] = preg_replace('/\&\#44/',',',trim($value));
$record[$header[$key]] = preg_replace($csv_encode,$csv_delimiter2,trim($value));
}
if ($record[$header[0]]) {
// add a new user to the database