Support Forums
Simple bbCode [TUT] - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21)
+---- Thread: Simple bbCode [TUT] (/showthread.php?tid=1676)



Simple bbCode [TUT] - Gaijin - 10-17-2009

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.




RE: Simple bbCode [TUT] - Viciousness - 10-17-2009

Good tutorial. There's a simple list of BBcode commands here that you may want to add.


RE: Simple bbCode [TUT] - Gaijin - 10-17-2009

(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.


RE: Simple bbCode [TUT] - zone - 11-08-2009

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


RE: Simple bbCode [TUT] - Gaijin - 11-08-2009

(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.


RE: Simple bbCode [TUT] - zone - 11-08-2009

(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


RE: Simple bbCode [TUT] - Gaijin - 11-08-2009

(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_expressions.php


RE: Simple bbCode [TUT] - zone - 11-08-2009

Thanks for the link Ninja, it is help ful really, I bookmarked the site and will learn some of the basics from there.


RE: Simple bbCode [TUT] - Bencori - 05-25-2011

Thank you. Found it useful and it replied to my question.


RE: Simple bbCode [TUT] - Carbon Host - 08-02-2011

great tutorial