phpnewlinenl2br

How to show just one newline using nl2br?


I am using nl2br() to convert \n characters to the <br /> tag but I do not want more than one <br /> tag at a time. For example, Hello \n\n\n\n Everybody should become Hello <br /> Everybody.

How can I do this?


Solution

  • The most direct approach might be to first replace the multiple newlines with one using a simple regular expression:

    nl2br(preg_replace("/\n+/", "\n", $input));