tags:

views:

2599

answers:

3

I need to remove blank lines (with whitespace or absolutely blank) in PHP. I use this regular expression, but it does not work:

$str = ereg_replace('^[ \t]*$\r?\n', '', $str);
$str = preg_replace('^[ \t]*$\r?\n', '', $str);

i want result of:

blahblah

blahblah

   adsa 


sad asdasd

will:

blahblah
blahblah
   adsa 
sad asdasd
A: 

what about this?

$str = preg_replace('^\s+\r?\n$', '', $str);
Jamie
+3  A: 
preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $string);
Michael Wales
This is also found here: http://programming-oneliners.blogspot.com/2006/03/remove-blank-empty-lines-php-29.html
Jamie
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string); this work
StoneHeart
A: 
$str = preg_replace('/^[ \t]*[\r\n]+/m', '', $str);
Alan Moore