使用php在新单元格中解析csv

时间:2013-10-18 12:15:03

标签: php csv

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

3 个答案:

答案 0 :(得分: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, ',');

你能告诉我这对你有帮助吗?

答案 1 :(得分: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

答案 2 :(得分: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];
}
?>