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 に答える