Is the limit of br nl2br shown in a row?

I'm having problems with the bb parser. I am coding. Or, well, not with the parser itself, but with nl2br changing it.

The row from the database is as follows:

text text text

[code] code of the code code [/ code]

text text text

Now nl2br puts one br / after the first "text text text", and then another one below it, so there are two line breaks before the [code] tag (which is actually correct, but not what I want).

Is it possible to limit the number of lines per line? I cannot find a solution simple enough.

Thanks guys.

+3
source share
3 answers

, . double <br> nl2br, .

$string = nl2br( $string );
$string = preg_replace( '/(<br(?: \\/)?>\\r?\\n?\\r?)(?=\\1)/is', '', $string );
+4

, , ( ) preg_replace: -)

0

you can use

$string = str_replace(array("\r\n\r\n", "\n\r\n\r", "\n\n", "\r\r"), array("\r\n","\n\r","\n","\r"), $string);

This prevents double <br> tags. Preg_replace, as suggested earlier, is better if there can be more than two new lines in a line.

0
source

All Articles