shieldcy
shieldcy

Reputation: 602

Retrieve multiple lines within same cell from CSV file

I am trying to retrieve some data from CSV file. I used the following code to go through CSV:

//create array for csv file
$array_data = array();
$row = 0;
if (($handle = fopen("import/pdf_data.csv", "r")) !== FALSE) {
    // read the CSV from root folder "web/interval/import"
    while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
        if ($row > 0) {
            $array_data[$row] = $data;
        }
        $row++;
    }
    fclose($handle);
}

//loop through all the feature data array
foreach ($array_data as $entry_value => $array_column) {
    foreach( $array_column as $value )
    {       
       //split data
       list($col1,$col2,$col3) = explode( ',', $value );

       echo "Name: ".$col1.", Surname: ".$col2.", Text: ".$col3."<br/>";

When I print the columns... col1 and col2 are fine as they have only one single value in their cells. col3 may contain multiple lines. Let's say for example (see below one cell of col3):

col3
::::::::::::::
Text-a in line 1
Text-a in line 2
Text-a in line 3

If there are multiple lines within one cell then the CSV output will be like this: "Text-a in line 1Text-a in line 2Text-a in line 3"

and with the code I use above it prints the first line "Text-a in line 1 then in a new entry the second line etc etc.

What I want to achieve is the following format

echo "Name: ".$col1.", Surname: ".$col2.", Text: ".$col3."<br/>";

Which doesn't work with multiple lines as I get this:

Any suggestions would be much appreciated, thank you

Upvotes: 2

Views: 4489

Answers (2)

SaidbakR
SaidbakR

Reputation: 13544

I could advice you by another simple solution. It is using fgets() and explode()

$handle = fopen("inputfile.txt", "r");
$out = array();
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $arr = explode(',' $line);
        $out[] = trim($arr[2]); // Supposing you want to get the third cell.
    }

    fclose($handle);
} else {
    // error opening the file.
}
print_r($out); 

Reference: How to read a file line by line in php

Upvotes: 0

astax
astax

Reputation: 1767

A CSV file may contain multi-line value in a cell. But it needs to be enclosed in quotes. Excel does this correctly - create a test file and save into CSV. PHP function fgetcsv also can read such files correctly.

Example file:

"col1","col2","col3"
"test1","test2.1
test2.2
test2.3","test3"

The second column in the second row contains multi-line value and this is a perfectly valid CSV file.


Here is the correct code:

$array_data = array();
$row = 0;
if (($handle = fopen("import/pdf_data.csv", "r")) !== FALSE) {
    // read the CSV from root folder "web/interval/import"
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        if ($row > 0) {
            $array_data[$row] = $data;
        }
        $row++;
    }
    fclose($handle);
}

//loop through all the feature data array
foreach ($array_data as $row) {
   list($col1,$col2,$col3) = $row;

   echo "Name: ".$col1.", Surname: ".$col2.", Text: ".$col3."<br/>";
}

Upvotes: 1

Related Questions