Thursday, October 21, 2010

Remove Blank/Empty lines from a string using PHP

Need to remove blank or empty lines from a string? Using PHP? Look no further.

Here's a quick function you can drop into your next PHP project to remove empty lines.

It's simply a regex but it captures many new line formats, enjoy!

function removeEmptyLines($string)
{
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);
}

0 comments:

Post a Comment