Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Multiple New Lines in One New Line

I want to replace multiple Newline characters with one Newline character, multiple spaces with a single space and Insert In to database.
so far I tried this preg_replace("/\n\n+/", "\n", $text); and doesn't work !

I also do this job on the $text for formatting.

    $text = wordwrap($text,120, '<br/>', true);
    $text = nl2br($text);
like image 670
BenjaBoy Avatar asked Nov 29 '22 11:11

BenjaBoy


1 Answers

Try using the following pattern:

/[\n\r]+/

as follows:

preg_replace( "/[\r\n]+/", "\n", $text );
like image 86
hjpotter92 Avatar answered Dec 05 '22 16:12

hjpotter92