php - fgetcsv skip blank lines in file -
i have script did, grabs files in "logs" folder , merge them in 1 array file, problem that, script breaks if there blank line or empty line! how can tell automatically skip blank empty lines , go next? blank lines not @ top or bottom! in middle of csv file
<?php $csv = array(); $files = glob('../logs/*.*'); $out = fopen("newfile.txt", "w"); foreach($files $file){ $in = fopen($file, "r"); while (($result = fgetcsv($in)) !== false) { $csv[] = $result; } fclose($in); fclose($out); } print json_encode(array('aadata' => $csv )); ?>
as can read in documentation fgetcsv()
:
a blank line in csv file returned array comprising single null field, , not treated error.
checking before adding data array should sufficient:
while (($result = fgetcsv($in)) !== false) { if (array(null) !== $result) { // ignore blank lines $csv[] = $result; } }
Comments
Post a Comment