Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple bbCode [TUT]
#1
What is bbCode
http://en.wikipedia.org/wiki/BbCode Wrote:Bulletin Board Code or BBCode is a lightweight markup language used to format posts in many message boards.
The available tags are usually indicated by square brackets surrounding a keyword,
and they are parsed by the message board system before being translated into a markup language that web browsers understand—usually HTML or XHTML.

Purpose
BBCode was devised to provide a safer, easier and more limited way of allowing users to format their messages.
Previously, many message boards allowed the users to include HTML formatting, a side effect of which was that malformed HTML could disrupt the page's layout,
or HTML could be used to run JavaScript leading to XSS attacks. Some implementations of BBCode have suffered problems related to the way they translate the BBCode into HTML,
which could negate the security that was intended to be given by BBCode. An alternative is a properly written HTML filter (many of which are freely available).
Although the basic tags of BBCode are similar in most internet forum software, there is currently no standards document for BBCode and there are consequently many incompatible variants.
Some variants are case-sensitive while others are not. Some variants have tags which highlight the formatting of HTML, ASP, PHP, SQL and other markup languages and programming languages.


In this tutorial I will show you a simple way of parsing bbCodes using Regular Expressions with the function preg_replace().
The function parses only b, url, color tags, I hope to get you to expand it your self to do more.
Functions
- preg_replace(mixed $pattern, mixed $replacement, mixed $subject [, int $limit = -1 [, int &$count]])

Start
For our bbCode parser all we need is one simple function.

PHP Code:
<?php

function bbCode($str) {
    
$str preg_replace('/\[b\](.*?)\[\/b\]/''<b>$1</b>'$str);
    
$str preg_replace('/\[url=([^ ]+).*\](.*?)\[\/url\]/''<a href="$1">$2</a>'$str);
    
$str preg_replace('/\[color=([[:alnum:]]{6}?).*\](.*?)\[\/color\]/''<font color="$1">$2</font>'$str);
    
    return 
$str;
}

?>

The first parameter of the preg_replace() function is used for matching pattern.
Code:
/\[b\](.*?)\[\/b\]/
/\[url=([^ ]+).*\](.*?)\[\/url\]/
/\[color=([[:alnum:]]{6}?).*\](.*?)\[\/color\]/

In the first we are lookgin for Bold text bbCode, we use the regular expression (.*?) to match every thing between opening and closing bbTag, the match is then saved to "$1" variable.
In the second we are lookgin for link replacement, in this tag we use 2 regular expressions, and they are saved in the row of their use, $1 would be the URL of our link and $2 the name of the link.
We use the ([^ ]+).* expression to match everything besides white space.
And in the third we want to allow changing of font color, we use ([[:alnum:]]{6}?).* to match alpha numeric characters with the total length of 6 characters.

That's it, simple isn't it?

All you have to do now is pass some text with bbCode in it to our function.
PHP Code:
print bbCode("This is [b]bold[/b] text with the [color=33cc66]green[/color] color and a link to [url=http://www.php.net]PHP main page[/url]"); 

A list of mostly used bbCodes can be found by following the link below.
Thanks to MreGSX Wrote:http://www.supportforums.net/misc.php?action=help&hid=7

Thank you for learning.

Reply
#2
Good tutorial. There's a simple list of BBcode commands here that you may want to add.
[Image: MreGSXsigcopy.png]

Reply
#3
(10-17-2009, 08:50 AM)MreGSX Wrote: Good tutorial. There's a simple list of commands here that you may want to add.

Thanks, I didn't know what to do so I've killed 15 minutes writing this., it's simple but it does the job.

And thanks for the link will add it.
Reply
#4
Very good tut.
I use this simple function whenever I like to parse the BBcode.
PHP Code:
<?
function bbcode($text){
   
$text str_replace('<''<'$text);
   
$text str_replace('>''>'$text);
   
$text str_replace('"'""", $text);
   
   
$text = str_replace('[b]', '<b>', $text);
   
$text = str_replace('[/b]', '</b>', $text);
   
$text = str_replace('[i]', '<i>', $text);
   
$text = str_replace('[/i]', '</i>', $text);
   
$text = str_replace('[u]', '<u>', $text);
   
$text = str_replace('[/u]', '</u>', $text);
   
$text = str_replace('[url=', '<a href="', $text);
   $text = str_replace('
[/url]', '</a>', $text);
   $text = str_replace('
[img]', '<img src="', $text);
   
$text = str_replace('[/img]', '">', $text);
   $text = str_replace('
[c]', '<center>', $text);
   $text = str_replace('
[/c]', '</center>', $text);
   $text = str_replace('
]', '">', $text);
   
   
$text = str_replace('[B]', '<b>', $text);
   
$text = str_replace('[/B]', '</b>', $text);
   
$text = str_replace('[I]', '<i>', $text);
   
$text = str_replace('[/I]', '</i>', $text);
   
$text = str_replace('[U]', '<u>', $text);
   
$text = str_replace('[/U]', '</u>', $text);
   
$text = str_replace('[URL=', '<a href="', $text);
   $text = str_replace('
[/URL]', '</a>', $text);
   $text = str_replace('
[IMG]', '<img src="', $text);
   
$text = str_replace('[/IMG]', '">', $text);
   $text = str_replace('
[C]', '<center>', $text);
   $text = str_replace('
[/C]', '</center>', $text);
   $text = str_replace('
]', '">', $text);
   
   
$text = nl2br($text);
   
$text = trim($text);
   
   return 
$text;

?>

Hopefully this is also an helpful idea Smile
Reply
#5
(11-08-2009, 07:44 PM)zone Wrote: Very good tut.
I use this simple function whenever I like to parse the BBcode.
Hopefully this is also an helpful idea Smile

Thanks, That's a nice code, but I would suggest you to use RegEx it would shorten your function and give you more control.
Reply
#6
(11-08-2009, 07:49 PM)Master of The Universe Wrote: Thanks, That's a nice code, but I would suggest you to use RegEx it would shorten your function and give you more control.

That's very true, but seriously, I am a fresh student of php Smile I am learning its detailed functions. Hopefully I will add this to my to do list to learn more about your RegEx()

Thanks Ninja Geek, be happy
Reply
#7
(11-08-2009, 07:58 PM)zone Wrote: That's very true, but seriously, I am a fresh student of php Smile I am learning its detailed functions. Hopefully I will add this to my to do list to learn more about your RegEx()

Thanks Ninja Geek, be happy

I have few tutorials in here for Beginners, and Tim's 101 is also good.
And here you can read about regex
http://www.webcheatsheet.com/php/regular...ssions.php
Reply
#8
Thanks for the link Ninja, it is help ful really, I bookmarked the site and will learn some of the basics from there.
Reply
#9
Thank you. Found it useful and it replied to my question.
Reply
#10
great tutorial
Superman
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  BBCode problem Horusbrasil 11 1,978 04-06-2010, 09:57 AM
Last Post: Horusbrasil

Forum Jump:


Users browsing this thread: 1 Guest(s)