Home > Development > PHP > BBCode 2

This is a class that is used to parse a string and format it for BBCode. This class implements the use of a unique identifier, for the purpose of saving resources post-database.

Requirements:

  • PHP version 4.3 or greater

Notes:

  • On this project, it is suggested that you download the latest version, as it will generally have the least amount of bugs.
  • Please leave bug comments on the respective version’s page.

Pre-database Operations:

<?php
include_once('bbcode.class.php');
 
$str    = '[i][b]hello[/b] world[/i]';
$bbcode = new bbcode($str, 'pre');
 
// store these variables in your database;
$db_input_str = $bbcode->parse();
$db_input_uid = $bbcode->uid;
 
echo $db_input_str . "<br />\n";
?>

Outputs:
[i:8dcc4488][b:8dcc4488]hello[/b:8dcc4488] world[/i:8dcc4488]

Post-database Fetch:

<?php
include_once('bbcode.class.php');
 
// fetch string and uid from database
$str = $db_input_str;
$uid = $db_input_uid;
 
$bbcode = new bbcode($str, 'post', $uid);
 
// echo the result
echo $bbcode->parse();
?>

Outputs:
<em><strong>hello</strong> world</em>

Usage Sans-Database:

Warning: This is highly inefficient.

<?php
include_once('bbcode.class.php');
 
$str = '[i][b]hello[/b] world[/i]';
 
$bbcode1 = new bbcode($str, 'pre');
$bbcode2 = new bbcode($bbcode1->parse(), 'post', $bbcode1->uid);
 
// echo the result
echo $bbcode2->parse();
?>

Outputs:
<em><strong>hello</strong> world</em>

Versions: