从文本文件中删除空白行

Jay*_*atz 4 php file

我有一个文本文件,其中有一些空行.意思是在它们上面没有任何东西的线条,只占用空间.

它看起来像这样:

The

quick
brown

fox jumped over
the

lazy dog
Run Code Online (Sandbox Code Playgroud)

我需要它看起来像这样:

The
quick
brown
fox jumped over
the
lazy dog
Run Code Online (Sandbox Code Playgroud)

如何删除这些空行并仅删除包含内容的行并将其写入新文件?

以下是我知道该怎么做:

$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");*/
}
Run Code Online (Sandbox Code Playgroud)

JRL*_*JRL 8

如果文件不是太大:

file_put_contents('newFile.txt',
                  implode('', file('tagged.txt', FILE_SKIP_EMPTY_LINES)));
Run Code Online (Sandbox Code Playgroud)