开发者

Limit amount of br's nl2br shows in a row?

I'm having some problems with a "bb parser" I'm coding. Or, well, not with the parser itself, but the nl2br modifying it.

The string from the database is like the following:

text text text

[code]code code code[/code]

text text text

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

Is there any way I can limit how many br's are entered in a ro开发者_如何学Pythonw? I can't seem to find a solution that's simple enough.

Thanks in advance, guys.


In addition to previous solution, I add a different one, since Fredrik asked for it. This will replace double <br> after nl2br instead of before.

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


You could for example replace two linebreaks (or more) by one by using preg_replace :-)


You could 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 before is better if there could be more than two new lines in a row.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜