Jump to content

BBCode Line Breaks


DrFishNips

Recommended Posts

I added a PHP BBCode parsing script to my site

function bbcode($replace)
{
$bbcode = array(
"'\[center\](.*?)\[/center\]'is" => "<center>\\1</center>",
"'\[left\](.*?)\[/left\]'is" => "<div style='text-align: left;'>\\1</div>",
"'\[right\](.*?)\[/right\]'is" => "<div style='text-align: right;'>\\1</div>",
"'\[pre\](.*?)\[/pre\]'is" => "<pre>\\1</pre>",
"'\[b\](.*?)\[/b\]'is" => "<b>\\1</b>",
"'\[quote\](.*?)\[/quote\]'is" => "<div class='top'><b>Quote:</b><hr>\\1</div>",
"'\[i\](.*?)\[/i\]'is" => "<i>\\1</i>",
"'\[u\](.*?)\[/u\]'is" => "<u>\\1</u>",
"'\[s\](.*?)\[/s\]'is" => "<del>\\1</del>",
"'\[move\](.*?)\[/move\]'is" => "<marquee>\\1</marquee>",
"'\[url\](.*?)\[/url\]'is" => "<a href='\\1' target='_BLANK'>\\1</a>",
"'\[url=(.*?)\](.*?)\[/url\]'is" => "<a href=\"\\1\" target=\"_BLANK\">\\2</a>",

"'\[drug=(.*?)\](.*?)\[/drug\]'is" => "<a href=\"?get=drugs&drug=\\1\" target=\"_BLANK\">\\2</a>",
"'\[drug_gb=(.*?)\](.*?)\[/drug_gb\]'is" => "<a class=\"gb_link\" href=\"?get=botany&page=preps&prep=\\1\" target=\"_BLANK\">\\2</a>",

"'\[prep=(.*?)\](.*?)\[/prep\]'is" => "<a href=\"?get=botany&page=preps&prep=\\1\" target=\"_BLANK\">\\2</a>",
"'\[prep_gb=(.*?)\](.*?)\[/prep_gb\]'is" => "<a class=\"gb_link\" href=\"?get=botany&page=preps&prep=\\1\" target=\"_BLANK\">\\2</a>",

"'\[mol=(.*?)\]'" => "<img border=\"0\" src=\"chemistry/molecules/\\1-sm.png\">",
"'\[3dmol=(.*?)\]'" => "<img border=\"0\" src=\"chemistry/molecules/\\1-sm3d.png\">",

"'\[img\](.*?)\[/img\]'is" => "<img border=\"0\" src=\"\\1\">",
"'\[img=(.*?)\]'" => "<img border=\"0\" src=\"\\1\">",
"'\[email\](.*?)\[/email\]'is" => "<a href='mailto: \\1'>\\1</a>",
"'\[size=(.*?)\](.*?)\[/size\]'is" => "<span style='font-size: \\1;'>\\2</span></a>",
"'\[font=(.*?)\](.*?)\[/font\]'is" => "<span style='font-family: \\1;'>\\2</span></a>",
);
$replace = preg_replace(array_keys($bbcode), array_values($bbcode), $replace);
return $replace;
}

 

It works well but it doesn't take line breaks into consideration. For example say I enter 2 paragraphs of info into my DB like this

Paragraph 1 BLAH BLAH BLAH

 

Paragraph 2 BLAH BLAH BLAH

 

It will be displayed as Paragraph 1 BLAH BLAH BLAHParagraph 2 BLAH BLAH BLAH

 

I can't expect users to manually type in [br] tags or something and all forums obviously handle paragraphs but how do they do it?

Link to comment
https://forums.phpfreaks.com/topic/163556-bbcode-line-breaks/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.