Wednesday, 4 September 2013

Deleting blank rows from a csv file and saving it using PHP

Deleting blank rows from a csv file and saving it using PHP

I have a csv file with 40031 rows. I have to delete all the blank rows in
the file. here i'hve written some PHP code for row 13 to row 40, but it's
not working. where is the problem?
My PHP Code:
<?php
function del_blank_row($filename, $startrow, $endrow){
$status = 0;
//check if file exists
if (file_exists($filename)) {
//end execution for invalid startrow or endrow
if ($startrow < 0 || $endrow < 0 || $startrow > 0 && $endrow > 0
&& $startrow > $endrow) {
die('Invalid startrow or endrow value');
}
$updatedcsv = array();
$count = 0;
//open file to read contents
$fp = fopen($filename, "r");
//get file contents in an array
$csvcontents = fgetcsv($fp);
//for every row
foreach ($csvcontents as $csvcontent) {
if (!empty($csvcontent)) {
array_push($updatedcsv, $csvcontent);
continue;
}
}
print_r($updatedcsv);
$fp = fopen($filename, "w");
fputcsv($fp, $updatedcsv);
fclose($fp);
} else {
die('File does not exist');
}
}
//-----call-----//
$csvdata=del_blank_row('h.csv', 13, 48);

No comments:

Post a Comment