What is BB code?
Bulletin Bar Code or simply BB Code is a lightweight markup language that is used to impose strict formatting rules in discussion boards, forums and occasionally in blogs.
Its main objective is to facilitate a controlled, secure and easy method of allowing users to format their messages. BB code eliminates the need of using HTML for formatting, the use of HTML for formatting may lead to vulnerabilities against SQL injection attacks.
In programming related websites BB Code can be used to highlight code from the rest of text. It can also be used to build javascript powered interfaces which in turn allow point-and-click content formatting.
PHP Implementation:
<?php
function BBdecodestring($text){
$find = array(
'~[b](.*?)[/b]~s',
'~[i](.*?)[/i]~s',
'~[u](.*?)[/u]~s',
'~[size=(.*?)](.*?)[/size]~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 BBdecodestring('This is [b]bold text[/b].');
//output:This is bold text
echo BBdecodestring('This text [i]italic[/i] text.');
//output: This is italic text
echo BBdecodestring('This is [u]underlined[/u] text.');
//output:This is underlined text
echo BBdecodestring('this is [size=18]18px[/size] font.');
//output:This is 18px font
?>