how to detect the csv delimiter while importing to db
Posted: Thu Feb 20, 2014 1:15 am
i wrote a php file to import a csv file directly to the mysql db. at the begining i wrote the code with fgetcsv and delimiter "\t" . but now there occurs a problem the user wants to import files with comma separated also(i mean he wants the pgm to support both type files). how can i detect the delimiter used in the importing file? or how can i modify my function so that it works also with comma delimiter.
Code: Select all
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE)
{
//print_r($data);
$i = 0;
$values = array();
foreach($dbkey as $k) {
if (!empty($data[$i])) {
if($k!='patient_date'){
$values[$k] = $data[$i];
$i++;
}
else{
$str=$data[$i];
$values[$k]=DateTime::createFromFormat('d/m/y H:i', $str)->format('Y-m-d');
$i++;
}
}
else{
$values[$k]='';
$i++;
}
}
$ks = "`" . implode("`, `", array_keys($values)) . "`";
$values = "'" . implode("', '", $values) . "'";
$sql = "INSERT INTO ".WP_eemail_TABLE_SUB." ({$ks}) VALUES ({$values})";
$wpdb->get_results($sql);
}