Allow only one br in nl2br

goo*_*ing 3 php

I let my members of my website to post some information about them in textarea. I use function nl2br to be it more prettier, smth like that:

$text_of_area = nl2br($_POST['text_area_name']); //yeah, of course i use functions against xss, sql injection attacks

mysql_query("..."); //here I insert a text
Run Code Online (Sandbox Code Playgroud)

But here is problem. I don't want to allow people to use more then one enter (br) allowed in text, so what can I do?

irc*_*ell 6

Why not just replace more than one new line away prior to calling nl2br?

If you want to let them use only one new line at all in their post:

$firstPos = strpos($text, "\n");
if ($firstPos !== false) {
    $text = substr_replace(array("\r","\n"),'', $text, $firstPos + 1);
}
$text = nl2br($text);
Run Code Online (Sandbox Code Playgroud)

If you want to let them use only one consecutive new line (allowing foo\nbar\nbaz):

$text = preg_replace('#[\r\n]+#', "\n", $text);
$text = nl2br($text);
Run Code Online (Sandbox Code Playgroud)