1

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

  • That would defeat pretty much the whole purpose of nl2br? – Fluffeh Nov 23 '14 at 19:29
  • @Fluffeh It appears that way to me but if a user adds a space between two lines of text, nl2br will double that space when echoed out –  Nov 23 '14 at 19:43
  • http://stackoverflow.com/questions/709669/how-do-i-remove-blank-lines-from-text-in-php – Cheery Nov 23 '14 at 20:10

1 Answers1

0

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 />
Atalatlae
  • 149
  • 4