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;
}