[英]How to import csv file to mysql in codeigniter
我有一个csv文件格式,如您所见,这里有一个“ 2013年1月7日”,我该如何将其转换为2013年1月7日。
WS-C3750X-48P-L,SOLAIRE,,FDO1651X2H9,"JAN. 7, 2013","JAN. 6, 2014",,1 Year,CISCO,Covered Under Warranty,30 Days,NA,"JAN. 2, 2013","APR. 2, 2014",1 Year and 3 Months,888-1,12643158
在csv文件中以这种格式出现的adadtional在我使用print_r()时显示,但在最上面的示例中没有插入。您能告诉我为什么吗?
GLC-SX-MM,SOLAIRE,,AGM1620L2LU,No Records,No Records,,NA,NA,NA,NA,NA,No Records,No Records,NA,No Records,No Records
这是我使用的库代码。
码:
class CSVReader {
var $fields; /** columns names retrieved after parsing */
var $separator = ';'; /** separator used to explode each line */
var $enclosure = '"'; /** enclosure used to decorate each field */
var $max_row_size = 4096; /** maximum row size to be used for decoding */
function parse_file($p_Filepath) {
$key_field = 'model,client_name,end_user,serial_num,trends_warranty_start,trends_warranty_end,client_contract_no,trends_warranty_period,vendor,support_coverage,sla,service_contract_num,support_coverage_start,support_coverage_end,support_coverage_period,trends_po,supplier_so';
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys_values = explode(',',$key_field);
$content = array();
$keys = $this->escape_string($keys_values);
$i = 0;
while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {
if( $row != null ) { // skip empty lines
$values = explode(',',$row[0]);
if(count($keys) == count($values)){
$arr = array();
$new_values = array();
$new_values = $this->escape_string($values);
for($j=0;$j<count($keys);$j++){
if($keys[$j] != ""){
$arr[$keys[$j]] = $new_values[$j];
}
}
$content[$i]= $arr;
$i++;
}
}
}
fclose($file);
return $content;
}
function escape_string($data){
$result = array();
foreach($data as $row){
$result[] = str_replace('"', '',$row);
}
return $result;
}
}
要将“ 2013年1月7日”转换为2013年1月7日。
$str = 'JAN. 7, 2013';
echo date("Y-m-d", strtotime($str))
尝试使用条件运算符来检查需要转换的字段。
如果我是你,我只会这样做
for($j=0;$j<count($keys);$j++){
if($keys[$j] != ""){
if ($j == 4 || $j == 5 || $j == 12 || $j == 13) {
$new_values[$j] = date("Y-m-d", strtotime($new_values[$j]));
$arr[$keys[$j]] = $new_values[$j];
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.