Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find consecutive new lines separated by spaces

Tags:

string

regex

php

I'm using str_replace("\n\n"); to find any two consecutive new lines \n but the problem is the text I'm working with isn't uniform. The new lines aren't aways back to back. They could be separated by any number of spaces or even a tab (found one). Don't like to use regex that much, but looks like I may need one here.

like image 810
sami Avatar asked Jan 01 '26 01:01

sami


1 Answers

Try this:

$output = preg_replace('/\n\s*\n/s', '', $input);
like image 136
Cameron Avatar answered Jan 02 '26 16:01

Cameron