I have a text file that has some blank lines in it. Meaning lines that have nothing on them and are just taking up space.
It looks like this:
The
quick
brown
fox jumped over
the
lazy dog
and I need it to look like this:
The
quick
brown
fox jumped over
the
lazy dog
How can I remove those blank lines and take only the lines with the content and write them to a new file?
Here is what I know how to do:
$file = fopen('newFile.txt', 'w');
$lines = fopen('tagged.txt');
foreach($lines as $line){
/* check contents of $line. If it is nothing or just a \n then ignore it.
else, then write it using fwrite($file, $line."\n");*/
}
If the file is not too large:
file_put_contents('newFile.txt',
implode('', file('tagged.txt', FILE_SKIP_EMPTY_LINES)));
file_put_contents('newFile.txt',
preg_replace(
'~[\r\n]+~',
"\r\n",
trim(file_get_contents('tagged.txt'))
)
);
I like \r\n
:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With