priyanka patel
priyanka patel

Reputation: 637

parse csv in cell with new line using php

I am trying to parse csv file using fgetcsv method in php. problem is that some cell having text which has new line character and because of that it breaks all text after new line into other cell's value. how can i resolve this issue?

my file having following data

label,value
identifier_6,"label2323
werffjdnfg
sdfsdfdsfg
dfgdfngdngd"

my code is

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

Upvotes: 0

Views: 5277

Answers (3)

Latheesan
Latheesan

Reputation: 24116

PHP has a built in function for reading CSV: fgetcsv

function:

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

usage:

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

returns assoc array of your CSV file data

Upvotes: 1

Rahul Gupta
Rahul Gupta

Reputation: 10141

Here is the code, I works correctly even if your column values contains commas or new line characters

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

Upvotes: 0

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, ',');

Can you let me know this will help you or not.

Upvotes: 3

Related Questions