0

I have an issue with my output.csv file. This is what it looks like currently. I'm fine with either creating a new csv file that is modified or just modifying this csv file, but not sure how to go about it.

cn,mail,telephonenumber,uid
,,,
admin,,,
"Isaac Newton",newton@ldap.forumsys.com,,newton
"Albert Einstein",einstein@ldap.forumsys.com,314-159-2653,einstein
"Nikola Tesla",tesla@ldap.forumsys.com,,tesla

I would like the output to be

cn,mail,telephonenumber,uid
admin,,,
"Isaac Newton",newton@ldap.forumsys.com,,newton
"Albert Einstein",einstein@ldap.forumsys.com,314-159-2653,einstein
"Nikola Tesla",tesla@ldap.forumsys.com,,tesla

As you can see the second row is completely deleted. I've tried the following, but it doesn't work (this is from Remove Blank ROWS from CSV files in php)

$lines = file("output.csv", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
$num_rows = count($lines);
foreach ($lines as $line) {
    $csv = str_getcsv($line);
    if (count(array_filter($csv)) == 0) {
        $num_rows--;
    }
}

EDIT:

Here's my current PHP

            $filename = "output.csv";
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment; filename="' . $filename . '"');

            $file = fopen("ad.csv","r");

            while(! feof($file))
              {
              print_r(fgets($file));
              }
            fclose($file);

So essentially, I have a csv file named "ad.csv" that contains the information I need to be put into "output.csv". I was wondering how you would edit the output.csv such that it sends the download after being edited to remove the blank lines?

Currently, with Nigel's code attached to the bottom, it doesn't edit the outputted csv file.

tobeydw
  • 107
  • 1
  • 12

1 Answers1

2

If when you identify an empty row, you can then remove that row from the lines in the original array. Then just put all the remaining lines back together to the same file...

$lines = file("output.csv", FILE_SKIP_EMPTY_LINES );
$num_rows = count($lines);
foreach ($lines as $lineNo => $line) {
    $csv = str_getcsv($line);
    if (count(array_filter($csv)) == 0) {
        unset($lines[$lineNo]);
    }
}
file_put_contents("output.csv", $lines);

I've removed FILE_IGNORE_NEW_LINES so that the new lines are kept in the array.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Works perfectly! Thanks Nigel. – tobeydw Jul 22 '20 at 19:15
  • Hey Nigel, I updated the question with an issue I ran into. If you could take a look, it'd be appreciated! – tobeydw Jul 22 '20 at 20:12
  • You seem to be opening the ad.csv and not the output.csv – Nigel Ren Jul 22 '20 at 20:18
  • The ad.csv contains the same information as the output.csv, sorry for the confusion. Upon print_r, it fills up the output.csv with the same information as ad.csv. The reason for this is because the ad.csv is filled up by a prior search function, and then I write all the ad.csv data to output.csv to be downloaded by the user. – tobeydw Jul 22 '20 at 20:22
  • Never mind, got it! I edited the ad.csv file before outputting it to output.csv thank you! – tobeydw Jul 22 '20 at 20:27