Introduction

Simple BB code parser is a simple, open source(MIT license) BB code parser written in PHP. It is basically one PHP class to include in your own PHP scripts. Out of the box it supports the following tags.

Supported Tags

  • [b] - Bold Text
  • [i] - Italic Text
  • [u] - Underlined Text
  • [s] - Strike through text
  • [quote] - Blockquote
  • [list] and [*] - Ordered lists
  • [url] - Links
  • [img] - Images
  • [color] - font color
  • [size] -font size

Other Features

Furthermore, it will automatically parse the text for email addresses and web addresses and make the clickable. It supports smilies(two out of the box), converts new lines to HTML line breaks, and protects against HTML injection.

How to use it

Using Simple BB code parser is, well simple. Basically, you just need to declare the class and throw some BB code at it. It will take care of the rest

$t = '[b]Bold Text[/b] email@email.com [color=red]Red Text[/color]';//Some BB code require_once('simple_bb_code.php');//Include the file with the class(download this file below) $bb = new Simple_BB_Code();//Create an instance of the parser class $res = $bb->parse($t);//and parse the string, $res will now contain the HTML equivalent

Quick Options

Everything under the "Other Features" section above can be turned off easily. Simply pass in the option to the class constructor. The order of options is convert newlines?, parse similies?,make links clickable?. They all default to true. So the following will not make email@email.com clickable.

$t = '[b]Bold Text[/b] email@email.com [color=red]Red Text[/color]';//Some BB code require_once('simple_bb_code.php');//Include the file with the class(download this file below) $bb = new Simple_BB_Code(true,true,false);//Create an instance of the parser class, but with out clickable links. $res = $bb->parse($t);//and parse the string, $res will now contain the HTML equivalent

Adding Tags

Simple BB code parser of course has a simple method to add new tags. The simpliest is the method addTag($bbTag,$htmlTag). For example, it could be used to add the tag [code]

$t = '[code]Some code that needs proper spacing[/code]';//Some BB code require_once('simple_bb_code.php');//Include the file with the class(download this file below) $bb = new Simple_BB_Code();//Create an instance of the parser class $bb->addTag('code','pre');//Tell the parser to parse [code] tags into pre html tags $res = $bb->parse($t);//and parse the string, $res will now contain the HTML equivalent

Or perhaps you want to add more smilies. That is just as simple. Use the method addSmiley($smiley,$url_to_image). So if you wanted :smile: to work like :) than you would do this.

$t = ':smile:';//Some BB code require_once('simple_bb_code.php');//Include the file with the class(download this file below) $bb = new Simple_BB_Code();//Create an instance of the parser class $bb->addTag(':smile:','smile.gif');//Add the :smile: $res = $bb->parse($t);//and parse the string, $res will now contain the HTML equivalent

There are two more methods addMapped and addTagWithAttribute, but they are much more advanced. Documentation of those two is coming soon

Download

You can get the current version here


Warning: WARNING: Comments is not properly cleaning input params. in /home/freegam4/public_html/webtech/lib/classes/class.module.inc.php on line 1500

Comments

Posted on July 1, 2008, 5:48 am by Jim

Great script! These are the classes we need :). Takes allot of work to create by yourself and needs allot of debugging. Hope it workes well!

Posted on May 21, 2008, 11:19 pm by test

tets

Posted on January 3, 2008, 11:35 am by Tom

[list] and [*] parsing does not work.

Posted on September 27, 2007, 11:38 am by Andres

Well I wonder if [b]here we have the [i]bbcode script[/i] actually running[/b], I hope it is!

Posted on August 25, 2007, 5:33 am by Lukas

Thanks for sharing! This is very useful.

Posted on August 22, 2007, 6:37 am by Andy Moore

Hi,

Thanks for this cool script, I've been working on something where I store values in an XML file and this is perfect for storing HTML as BBCode rather than breaking the XML with more tag soup.

Worked perfect first time. Thanks.

Andy

Posted on July 11, 2007, 8:11 am by Late

Great program!

Posted on April 17, 2007, 1:45 pm by Brady

Thanks, Mike!

Posted on March 31, 2007, 3:27 pm by Mike

Wow... amazing script Brady. (By the way, this is mburt from dynamicdrive.com forums). I tried a php bbcode script on my own, and could only get the basic stuff. I use it on my blogging site (creates blogs): http://livejournal.funpic.org

Add A Comment

What does two minus two equal?
Your Name(*):
Email:
Comment(*):