[英]Deleting blank rows from a csv file and saving it using PHP
我有一個40031行的csv文件。 我必須刪除文件中的所有空白行。 在這里,我已經為第13行到第40行編寫了一些PHP代碼,但是沒有用。 問題出在哪兒?
我的PHP代碼:
<?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);
首先以讀取模式關閉文件,然后以寫入模式打開! 那是放一個fclose($fp);
$fp = fopen($filename, "w");
之前的語句$fp = fopen($filename, "w");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.