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?
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?
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));
If you have php 5.2.4+ you can use preg_replace and the vertical whitespace character type \v
$str = preg_replace('/\v+/','<br>', $str);
I'd try replacing repeated newlines with single newlines using preg_replace() first, then using nl2br to convert to HTML
tags. nl2br(preg_replace('/\n+/', '\n', $the_string))
should do the trick (untested).