BBcode with PHP

What is BB Code?

Bulletin Board Code or BB Code is a lightweight markup language used to format text within posts made on the majority of message board platforms, although it’s now widely used in any application that permits posts, comments or contributions from users. The available BBcode tags (similar to HTML) are usually indicated by square brackets surrounding a keyword, and they are parsed server-side before being translated into a markup language that web browsers can render – such as HTML. BB tags can be typed manually around text content to be formatted; alternatively, developers can build JavaScript-powered interfaces to allow for simple point-and-click content formatting.

BBcode originated from its early use in Ultimate Bulletin Board – widely considered to be the first real discussion forum. Since that time the majority or major discussion and bulleting boards (such as vBulletin and phpBB) have adopted BB code in one way or another.

Why use BBcode instead of HTML? In most cases, allowing users to enter HTML inside their message is impractical for both security and stylistic reasons. BB Code is used as an alternative to raw HTML and limits the kind of formatting that can be applied to text in a post.

There is no industry standard that applies to BB code so the implementation is essentially up to the discretion of the application designer. It also means that there are countless variations of the code in use.. with code that performs ‘non-standard’ functionality such as a tag that will include a YouTube vide, to a tag that will render highlighted PHP code.

Some basic examples of BB code

Code: This is an example of [b]bold[/b]
Result: This is an example of bold

Code: This is an example of [u]underlined[/u] text and this is text in [i]italics[/i].
Result: This is an example of underlined text and this is text in italics

PHP functions:

Below are two functions that permit you to implement BBcode into your basic applications.

This first (cheap & nasty) function simply demonstrates the effectiveness of the PHP str_replace function.

<?php

function basicbbcode($text) {
$text = str_replace("[b]", "<b>", "$text");
$text = str_replace("[/b]", "</b>", "$text");
$text = str_replace("[u]", "<u>", "$text");
$text = str_replace("[/u]", "</u>", "$text");
$text = str_replace("[i]", "<i>", "$text");
$text = str_replace("[/i]", "</i>", "$text");
return $text;
}

echo basicbbcode('This is [b]bold[/b] and this is [u]underlined[/u] and this is in [i]italics[/i].');
?>

The above will output “This is bold and this is underlined and this is in italics.

This second funtion is faster and more robust than the simple implementation above. It uses a $find array and a $replace array, and then uses the PHP preg_replace function to generate your text.

br>

<?php
function txtbbc($text){
    $find = array(
        '~\[b\](.*?)\[/b\]~s',
        '~\[i\](.*?)\[/i\]~s',
        '~\[u\](.*?)\[/u\]~s',
        '~\[size=(.*?)\](.*?)\[/size\]~s',
        '~\[color=(.*?)\](.*?)\[/color\]~s'
    );
    $replace = array(
        '<b>$1</b>',
        '<i>$1</i>',
        '<span style="text-decoration:underline;">$1</span>',
        '<span style="font-size:$1px;">$2</span>',
        '<span style="color:$1;">$2</span>'
    );
    return preg_replace($find,$replace,$text);
} 

echo txtbbc('This is [b]bold text[/b].');
echo txtbbc('This text [i]italic[/i] text.');
echo txtbbc('This is [u]underlined[/u] text.');
echo txtbbc('this is [size=18]18px[/size] font.');
?>

Strip BBcode from text

If you want to query a database and strip BBcode from text you can use this function.

<?php
function stripBBCode($text_to_search) {
$pattern = '|[[\/\!]*?[^\[\]]*?]|si';
$replace = '';
return preg_replace($pattern, $replace, $text_to_search);
}
?>

Links:

Pear BBCodeParser package.
BBcode explained at phpBB.com.
BBcode Classes at phpClasses.org (membership required).

If you liked this article, you may also like:

  1. Make text links clickable
  2. [Shortcode] Examples [2]
  3. Find and Replace Words in a WordPress Post
  4. Aviation METAR with PHP
  5. PHP Function to Force Links to Open in a New Window
About Marty

is a passionate web developer from Sydney, Australia. He owns about 400 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. You can follow Marty on Twitter or Google+.

Comments

  1. Pet says:

    Nice & easy….. youve saved me a lot of work researching something i knew nothing aboiut. Cheers.

  2. Elio Tosa says:

    YOU HAVE SAVED MY DAY. Thank you :)

  3. Eric Bunner says:

    Exactly what I was looking for. Thank you.

  4. Tom Krsticic says:

    You’ve saved me many hours. Thank you.

  5. Michiel says:

    Nice,
    But how will this work if you combine bbcodes to 1 piece of text.

    like this [Color=red][font-size=12]Hello[/color][/size]
    This will outut
    hello

  6. William says:

    Great function. Works great even for a PHP nooob like me. Thanks.

Speak Your Mind

*