0

我正在尝试使用fgetcsvphp 中的方法解析 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;
    }
4

3 回答 3

2
function get2DArrayFromCsv($file,$delimiter) 
{ 
        if (($handle = fopen($file, "r")) !== FALSE) { 
            $i = 0; 
            while (($lineArray = fgetcsv($handle, 10000, $delimiter)) !== FALSE) { 
                for ($j=0; $j<count($lineArray); $j++) { 
                    $data2DArray[$i][$j] = $lineArray[$j]; 
                } 
                $i++; 
            } 
            fclose($handle); 
        } 
        return $data2DArray; 
    } 
    $resList=get2DArrayFromCsv($csv_file, ',');

你能告诉我这对你有没有帮助。

于 2013-10-18T12:26:31.140 回答
1

PHP 有一个用于读取 CSV 的内置函数:fgetcsv

功能:

function parseCSV($file, $buffer = 1024, $delimiter = ',', $enclosure = '"') {
    $csv_data = array();
    if (file_exists($file) && is_readable($file)) {
        if (($handle = fopen($file, 'r')) !== FALSE) {
            while (($data = fgetcsv($handle, $buffer, $delimiter, $enclosure)) !== FALSE) {
                $csv_data[] = $data;
            }
        }
    }
    return $csv_data;
}

用法:

$csv_data = parseCSV('my_file.csv');

returns assoc array of your CSV file data

于 2013-10-18T12:33:39.050 回答
0

这是代码,即使您的列值包含逗号或换行符,我也能正常工作

<?php
// Set path to CSV file
$csvFile = 'test.csv';
$file_handle = fopen($csvFile, 'r');
//fgetcsv($file_handle);//skips the first line while reading the csv file, uncomment this line if you want to skip the first line in csv file
while (!feof($file_handle) )
{
    $csv_data[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);

echo '<pre>';
print_r($csv_data);
echo '</pre>';

foreach($csv_data as $data)
{
    echo "<br>column 1 data = ".$data[0];
    echo "<br>column 2 data = ".$data[1];
}
?>
于 2013-10-18T12:33:33.833 回答