Remove Blank ROWS from CSV files in php

15,065

Solution 1

$lines = file("test.csv", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
$num_rows = count($lines);
foreach ($lines as $line) {
    $csv = str_getcsv($line);
    if (empty($csv[SPECIFIC_COLUMN])) {
        $num_rows--;
    }
}

If you don't want to check a specific column, but just filter out rows where all columns are empty, change it to:

    if (count(array_filter($csv)) == 0) {

Solution 2

For empty rows:

$lines = file("test.csv", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
$num_rows = count($lines);
foreach ($lines as $line) {
    $csv = str_getcsv($line);
    if (!array_filter($csv)) {
        $num_rows--;
    }
}
Share:
15,065
telexper
Author by

telexper

Updated on July 31, 2022

Comments

  • telexper
    telexper almost 2 years

    Is it possible to remove all blank ROWS from a CSV file?

    I'm trying to count all rows from a CSV file but would like to exclude lines that doesn't contain a value on a specific column or the whole row.

    This is what I'm using to count the rows as of the moment.

    $import = file($target_path, FILE_SKIP_EMPTY_LINES);
    $num_rows = count($import);
    echo $num_rows;
    

    sample:

    Jun,Bronse,137 Raven,Princeton,TX,75407,2147088671,[email protected],1990,CHEVROLET,K1500,,
    ,,,,,,,,,,,,
    ,,,,,,,,,,,,
    ,,,,,,,,,,,,
    Nella,Brown,111 Venna St,Princeton,TX,75407,2147177671,[email protected],1993,CHEVROLET,K1500,,
    Jun,Bronse,137 Raven,Princeton,TX,75407,2147088671,[email protected],1990,CHEVROLET,K1500,,
    ,,,,,,,,,,,,
    Jun,Bronse,137 Raven,Princeton,TX,75407,2147088671,[email protected],1990,CHEVROLET,K1500,,
    
    • sjagr
      sjagr over 10 years
      empty() along with foreach() and explode() are a good place to start
  • sjagr
    sjagr over 10 years
    Empty row could be stored in the CSV as ,,,,,,,, (for example.) Your code would produce inaccurate results. Not to mention @telexper has already used FILE_SKIP_EMPTY_LINES
  • sjagr
    sjagr over 10 years
    +1 for str_getcsv(), never even heard of it before. I'm assuming the advantage of that function compared to explode() is that it would handle strings with commas in them correctly? (e.g. 1,2,"Here's a string, check it out",3,4)
  • telexper
    telexper over 10 years
    how about just getting a blank row not a specific column?
  • sjagr
    sjagr over 10 years
    @telexper !array_filter($csv) would return true if all values of the array are empty, but could become time consuming (better to just use a simple foreach)... That part is easy
  • Barmar
    Barmar over 10 years
    If all the columns are blank, the specific column will be blank, so that check will do it.
  • sjagr
    sjagr over 10 years
    @Barmar but not exclusively for an entirely blank row
  • telexper
    telexper over 10 years
    Can't use function return value in write context in /.../... on line "if (empty(array_filter($csv)))"
  • Grischa
    Grischa almost 10 years
    @ Barmar I have a similiar question: Link
  • Louis Eloy
    Louis Eloy almost 6 years
    This has been useful to me as of august 2018.