Create PHP BBCodes I

When I first planned this latest re-design to the website, one of my top priorities was for me to be able to use PHP BBCode style formatting in my articles - you know, the ones popularly used on Bulletin Boards and Forums these days?

What? You use BBCode to publish your articles here?

I'm afraid so... and it's proving to be the best decision I made so far this year :twisted:. Back to the topic - so, yes BBCodes... What are BBCodes? In case you don't know what I am talking about, here's what it looks like when you want a word to be in bold using BBCode:

Sample BBCode

code:
This is how you [b]bold[/b] a word!
This is how you [u]underline three words[/u]!

Resulting in something like this:

This is how you bold a word!
This is how you underline three words!

Very simply put, all BBCode does is allow just about anybody to post something to your website and still control how it will look like without the use of HTML (for security reasons). Of course I do not have guest writers yet but I try to plan for the future :p Having something like this in place will make it easy for these Guest Writers to post News and Articles anytime and from any PC!

So, how do I create and use BBCodes on my website?

This is what I intend to share with you in this multi-part article. What I actually created and use on this website today is a 200 line BBCode class file written in PHP. Perhaps someday soon, I will offer this 'BBCode parsing PHP Class' file as a download but for now I will try to point you in the same direction I took with some basic information...

Top PHP function to use - STR_REPLACE()

The very good news is that up to 75% of your BBCode-parsing PHP script is going to done with just one function perhaps; just using str_replace() to find BBCodes and replace them with HTML!

We start our BBCode-parsing script by 'assigning' our opening and closing BBCodes in an Associative Array, where we also include the HTML replacement codes like I have done below:

php:


<?php

//  First we assign our 'BBCodes' in an
//  Associative Array.
//  -----------------------------------
$bb_codes = array(
    # bold
  '[b]' => '<span style="font-weight:bold">',
  '[/b]' => '</span>',
    # italic
  '[i]' => '<span style="font-style:italic">',
  '[/i]' => '</span>',
    # underline
  '[u]' => '<span style="text-decoration:underline">',
  '[/u]' => '</span>',
    # smilies
  ':)' => '<img src="/images/smilie.gif" />',
  ':(' => '<img src="/images/sad.gif" />'
    # note the missing comma after the last line above
);

//  -----------------------------------

?>


Introducing our main BBCode-parsing function

This function is very simple to interpret, it just takes any text you submit to it, takes the entire list of BBCodes from our $bb_codes array above and cycles through them one at a time - searching and then replacing at each cycle and each occurence of a BBCode.

php:


<?php

function convert_bbcodes( $t ) 
{ 
  $search = array_keys( $GLOBALS['bb_codes'] );
  $t = str_replace( $search, $GLOBALS['bb_codes'], $t );
  return $t;
}

?>


Let's take a quick look at how you might use this in a sample PHP page before we get onto the next page.

php:


<?php 

//  Filename: test.php 
//  ================== 

//  Our sample text 
$text = 'I want this to be in [b]bold[/b] and this ' 
       .'to be in [b]bold[/b] too.  While that is ' 
       .'being done, how about something ' 
       .'[u]underlined[/u]? :) :('; 

//  Now we pass our sample text through the function. 
echo '<p>'.convert_bbcodes( $text ).'</p>'; 

//  =================================================
//  now we paste our CONVERT_BBCODES() function below 
//  ================================================= 

function convert_bbcodes( $t ) 
{ 
  $search = array_keys( $GLOBALS['bb_codes'] );
  $t = str_replace( $search, $GLOBALS['bb_codes'], $t );
  return $t;
}
?>


Part II - coming soon...

TOP

Do you like the new design?

 Yes

 No

 Don't know