Remove blank lines from a file

From: Date: Fri, 21 May 2010 12:03:01 +0000
Subject: Remove blank lines from a file
Groups: php.general 
Request: Send a blank email to php-general+get-305384@lists.php.net to get a copy of this message
Hi Im trying do something like this, have a function which uploads my file and returns file pointer ... but at same time ... I want to remove all Blank lines in a file and update it before it goes to the final location ... What I tried was to do a write of file and use some regexp replace to remove a blank ... either I am not doing the replace correct or my understanding of the file buffer and what I can do with it between the browser and saving is not correct, Anyway my code looks something like this : $uploadfile = $this->uploaddir; $mtran = mt_rand(999,999999); $NewName = date("Ymd_Gis").$mtran.".csv"; $uploadfile = $uploadfile.$NewName; try{ if (move_uploaded_file($_FILES['attachfile']['tmp_name'], $uploadfile)) { $handle = fopen($uploadfile, "r+"); $lines = file($uploadfile, FILE_SKIP_EMPTY_LINES); //FILE_IGNORE_NEW_LINES | foreach ($lines as $line_num => $line) { $line = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $line); if(strlen($line) > 0) $line=trim($line); $line=$line."\n"; fwrite($handle, $line); } fclose($handle);

« previous php.general (#305384) next »