0

In my code I'm reading csv file with fgetcsv function

while($line = fgetcsv($handle, 4096)){
    .....
}

How can check when the $line is empty, I mean $line = ',,,,,,,,' ?
note: I'm not always know the number of columns.

NickF
  • 5,637
  • 12
  • 44
  • 75

2 Answers2

1

One way to do it would be:

if (str_replace(array(',', ' '), '', $line) != '') {
  // do something
}

Basically, it will compare the line after you remove all comma's and spaces from the string.

Nick
  • 6,316
  • 2
  • 29
  • 47
0

You can take the contents of file in an array and check which index has empty value. like this -

$handle = fopen("file.csv","r");
$data = fgetcsv($handle,",");
while($data = fgetcsv($handle)) 
{
$array = explode(",",$data[0]);
print_r($array);
}
fclose($handle);
analyticalpicasso
  • 1,993
  • 8
  • 26
  • 45
Avneesh
  • 149
  • 1
  • 1
  • 7