Quantcast
Channel: How to remove a line from text file using php without leaving an empty line - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Thusitha Sumanadasa for How to remove a line from text file using php without leaving an empty line

$
0
0
     $DELETE = "the_line_you_want_to_delete";     $data = file("./foo.txt");     $out = array();     foreach($data as $line) {         if(trim($line) != $DELETE) {             $out[] = $line;         }     }     $fp = fopen("./foo.txt", "w+");     flock($fp, LOCK_EX);     foreach($out as $line) {         fwrite($fp, $line);     }     flock($fp, LOCK_UN);     fclose($fp);  

this will just look over every line and if it not what you want to delete, it gets pushed to an array that will get written back to the file.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>