user11940263
user11940263

Reputation:

excel file read rows and columns - php

$arr=array();
$row = -1;
if (($handle = fopen("out.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);

        $row++;
        for ($c = 0; $c < $num; $c++) {
            $arr[$row][$c]= $data[$c];
        }
    }
    fclose($handle);
}

I'm using this code to read excel file data, this code counts elements in a row that are divided by comma (,),

Name, Surname, Num, Tel

  1. Name
  2. Surname
  3. Num
  4. tel

but in one field I have word Orginal, and this code also divides element by that word like this:

  1. Orgina
  2. l

and in that way, I receive wrong elemnts, any help?

Upvotes: 0

Views: 781

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57131

In the line

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

the 1000 is the maximum length of data to read, as your data (in the example posted) has more than that, it will split the data.

To allow it to read the data properly you can just leave the second and third parameters as defaults ( null for length - in other words any length and the default delimiter is a comma anyway...

while (($data = fgetcsv($handle)) !== FALSE) {

Upvotes: 1

Related Questions