简体   繁体   中英

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?

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) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM