php - Retrieve multiple lines within same cell from CSV file -


i trying retrieve data csv file. used following code go through csv:

//create array csv file $array_data = array(); $row = 0; if (($handle = fopen("import/pdf_data.csv", "r")) !== false) {     // read csv root folder "web/interval/import"     while (($data = fgetcsv($handle, 0, ";")) !== false) {         if ($row > 0) {             $array_data[$row] = $data;         }         $row++;     }     fclose($handle); }  //loop through feature data array foreach ($array_data $entry_value => $array_column) {     foreach( $array_column $value )     {               //split data        list($col1,$col2,$col3) = explode( ',', $value );         echo "name: ".$col1.", surname: ".$col2.", text: ".$col3."<br/>"; 

when print columns... col1 , col2 fine have 1 single value in cells. col3 may contain multiple lines. let's example (see below 1 cell of col3):

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

if there multiple lines within 1 cell csv output this: "text-a in line 1text-a in line 2text-a in line 3"

and code use above prints first line "text-a in line 1 in new entry second line etc etc.

what want achieve following format

echo "name: ".$col1.", surname: ".$col2.", text: ".$col3."<br/>"; 

which doesn't work multiple lines this:

  • name: test, surname: test2, text: "text-a in line 1
  • name: , surname: , text: text-a in line 2

any suggestions appreciated, thank you

a csv file may contain multi-line value in cell. needs enclosed in quotes. excel correctly - create test file , save csv. php function fgetcsv can read such files correctly.

example file:

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

the second column in second row contains multi-line value , valid csv file.


here correct code:

$array_data = array(); $row = 0; if (($handle = fopen("import/pdf_data.csv", "r")) !== false) {     // read csv root folder "web/interval/import"     while (($data = fgetcsv($handle, 0, ",")) !== false) {         if ($row > 0) {             $array_data[$row] = $data;         }         $row++;     }     fclose($handle); }  //loop through feature data array foreach ($array_data $row) {    list($col1,$col2,$col3) = $row;     echo "name: ".$col1.", surname: ".$col2.", text: ".$col3."<br/>"; } 

Popular posts from this blog