【问题标题】:parse csv in cell with new line using php使用php用新行解析单元格中的csv
【发布时间】:2013-10-27 06:48:36
【问题描述】:

我正在尝试使用 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;
    }

【问题讨论】:

标签: php csv


【解决方案1】:
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, ',');

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

【讨论】:

    【解决方案2】:

    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

    【讨论】:

    • 他说他正在使用fgetcsv()
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多