我正在尝试使用fgetcsv
php 中的方法解析 csv 文件。问题是某些单元格的文本具有换行符,因此它将换行后的所有文本分解为其他单元格的值。我该如何解决这个问题?
我的文件有以下数据
label,value
identifier_6,"label2323
werffjdnfg
sdfsdfdsfg
dfgdfngdngd"
我的代码是
function parse_file($p_Filepath) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys_values = explode(',',$this->fields[0]);
$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);
$data['keys'] = $keys;
$data['csvData'] = $content;
return $data;
}
function escape_string($data){
$result = array();
foreach($data as $row){
// $result[] = $row;
$result[] = str_replace('"', '',$row);
}
return $result;
}