从文本文件中删除空白行 [英] Remove blank lines from a text file

查看:79
本文介绍了从文本文件中删除空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中有一些空白行.意思是没有任何内容的行仅占用空间.

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.

它看起来像这样:

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?

这是我知道该怎么做:

$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");*/
}

推荐答案

如果文件不是太大:

file_put_contents('newFile.txt',
                  implode('', file('tagged.txt', FILE_SKIP_EMPTY_LINES)));

这篇关于从文本文件中删除空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆