Regular Expressions 101

Community Patterns

Remove blank lines

1

Regular Expression
PCRE (PHP <7.3)

/
(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+
/
g

Description

Removes all blank lines.

PHP: trim(preg_replace("/(^[\r\n]|[\r\n]+)[\s\t][\r\n]+/", "\n", $string));

You have replace with a newline char to break non-blank lines. This also leaves a single trailing newline, easily removed with trim($string); if needed.

Submitted by anonymous - 8 years ago