Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

BBCode - Line Breaks and Paragraphs



5:29 pm on Apr 24, 2010 (gmt 0)

5+ Year Member

I have recently coded out a BBCode system for parts of my website using preg_replace and function, and all of it works when it comes to creating tags which require input between them. However, since I created this BBCode file and included it in my page, it seems to override my former str_replace property I used to automatically convert line breaks to <br /> tags and paragraphs to </p><p> tags in the appropriate spots. I have removed this part of code now, since it doesn't function at all with the BBCode include.

So, I have inserted the similar code for paragraph conversion into my BBCode file and this is what I have:


function bbcode($input){
$input = strip_tags($input);
$input = htmlentities($input);

$search = array(

$replace = array(

'<span style="font-weight:bold">$1</span>',
'<span style="font-style:italic">$1</span>',
'<span style="text-decoration:underline">$1</span>',
'<a href="$1">$2</a>',
'<a href="$1" target="_blank">$2</a>',


The following lines focus on the line to paragraph conversion:




I use the return between the paragraph tags to make the HTML markup tidy when viewing the source of the page.

That's all fine - works perfectly if I have a blank line in between text and the paragraphs are automatically put into place. However, when I try adding another code to represent single line breaks, the BBCode function is forced to place the <br /> tag in the right place, however the paragraph code kicks in and treats this tag as another blank line in addition to the new line I started using the 'return' key.

I can sort of understand why it's doing this, but I'm wondering if there is a solution and what it could be. My hunch is an if statement to tell the paragraph code not to execute when only one line break is present. I'm also wondering is str_replace could be used in the BBCode file as that was working perfectly before and treated new manually typed lines as a single <br /> tag and two as the paragraph tag.

If anybody could help with this I'd appreciate it a lot, thanks. :)


6:14 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

This is what I do:

$symbols = array(
'/[\r\n]{4,}/m' => '</p><p>',
'/[\r\n]{2,}/m' => '<br />'

foreach($symbols as $symb => $repl) {
$input = preg_replace($symb, $repl, $input);

May seem a bit wierd the loop/array etc, but hey, I just cut it out of my function that has ~100 items in that array :)


6:56 pm on Apr 24, 2010 (gmt 0)

5+ Year Member

I added the search (/[\r\n]{4,}/is and /[\r\n]{2,}/is) and replacements (</p><p> and <br />) to my code and it works wonders so thanks a lot for that. :)

Although (and call me fussy, hehe), is it possible to separate the paragraphs in the source code? So going from this:

<p>This is paragraph 1.</p><p>This is another one.</p><p>We are all cramped together</p>

to this:

<p>This is paragraph 1.</p>

<p>This is another one.</p>

<p>We are free.</p>

Since I was editing the replacement tags, the other part of the code considers the blank space between these paragraph tags as another line break so puts more spaces between the text. Is it possible to say:

  • If there is only one line break, add <br />.
  • If there are two line breaks, add </p> to the end and <p> on the next line where text is found.
  • If there are three or more line breaks, put the paragraph tags in appropriate spots and <br /> tags between them (like valid HTML).

If this isn't possible I wouldn't mind using the automatic paragraph tags after two line breaks, and insert <br /> tags myself, but the HTML tags don't work since I'm replacing them with BBCode. Taking out strip_tags causes the HTML markup to show in the article.

Also, is there a way to add BBCode which only requires one tag? For example, like in this forum use [ hr ] instead of [ hr ][/hr]. And for an easy method of enabling custom line breaks, add [ br ] instead of [ br ][/br] (ommitting the spaces). The latter does work but I'd rather type a single tag or I might as well use HTML markup.


7:32 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

I'll tell you what, I'll send you a link to a .phps that I've shared with a few people before, it's the function I use for parsing textarea/input-text, with the optional allowance for HTML and BB code aswell.

Check your stickymail inbox.


1:04 am on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Readie, you can call preg_replace just once, passing an array of patterns and a corresponding array of replacements.


11:19 am on Apr 25, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

I know four - however I call my function on the cron job/a new news post/edit, and a new comment and cache the formatted output on the server and just serve that to visitors, so the higher CPU usage brought by foreach() isn't really an issue.

Having it as '/search/' => 'replace', just makes it neater, and easier to maintain.

Featured Threads

Hot Threads This Week

Hot Threads This Month