How can empty lines be removed from a CSV file in PHP?
Empty lines in a CSV file can be removed in PHP by reading the file line by line, checking if each line is empty, and only writing non-empty lines to a new file. This can be achieved by using functions like `fopen`, `fgets`, and `fwrite`.
$inputFile = 'input.csv';
$outputFile = 'output.csv';
$in = fopen($inputFile, 'r');
$out = fopen($outputFile, 'w');
while (($line = fgets($in)) !== false) {
if (trim($line) != '') {
fwrite($out, $line);
}
}
fclose($in);
fclose($out);
echo 'Empty lines removed from CSV file.';
Keywords
Related Questions
- What potential pitfalls should be avoided when working with option fields in PHP forms to prevent data loss or incorrect display of selected options?
- What are some best practices for optimizing the selection of data from a MySQL database using PHP?
- What are the best practices for handling numeric values and calculations in PHP scripts?