在我的PHP博客中解析文本和代码的最佳方法是什么?

Usually, I use nl2br() and it does come out just like it's entered in the textarea, but this causes a problem when using bbcode or posting code in <code> or <pre> tags, since it adds extra line breaks.

For example this code

[sub-title]test[/sub-title]

some text here.

I'd like it to look like that when displayed in the browser, but because [sub-title] becomes <div class="sub-title"> the <br /> adds an extra line break, so it will look like this (with 2 line breaks in between)

**test**


some text here.

I haven't fully looked into it yet, but could the PHP bbcode parser help, or is the only/best solution to use regex?

You can use nl2br()

Example

$message =  nl2br(preg_replace('#(\\]{1})(\\s?)\
#Usi', ']', stripslashes($message)));