我需要在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) 我有一个文本文件,其中有一些空行.意思是在它们上面没有任何东西的线条,只占用空间.
它看起来像这样:
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)