相关疑难解决方法(0)

如何从PHP中删除文本中的空白行?

我需要在PHP中删除空白行(用空格或绝对空白).我使用这个正则表达式,但它不起作用:

$str = ereg_replace('^[ \t]*$\r?\n', '', $str);
$str = preg_replace('^[ \t]*$\r?\n', '', $str);
Run Code Online (Sandbox Code Playgroud)

我想要的结果:

blahblah

blahblah

   adsa 


sad asdasd
Run Code Online (Sandbox Code Playgroud)

将:

blahblah
blahblah
   adsa 
sad asdasd
Run Code Online (Sandbox Code Playgroud)

php regex

33
推荐指数
6
解决办法
7万
查看次数

从文本文件中删除空白行

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

它看起来像这样:

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)

php file

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

php ×2

file ×1

regex ×1