PHP ignore new lines that are blank characters when using nl2br

555 Views Asked by At

If the new line contains only blank characters or is an empty string, how can it be ignored when using nl2br?

1

There are 1 best solutions below

0
On

replace 2 or more continues \n and empty lines for only one \n

<?php
$text = "Hello
[tab]
[spaces]
      world

";
echo nl2br(preg_replace("/\W+\n+/", "\n", $text));

return

Hello<br />
world<br />