BBcode using a DB.

Started on the old forum in October 2009 · view the original in the archive · old links redirect here

No replies · 772 views · started 17 years ago · 5 min read

riderz Lead · 975 posts #1

Warning, there are not added protection to sql injections. So use mysql_real_escape_string() and htmlspecialchars() Before going though this function! Though it is recommended you do not use this function for inserting into DB, but for outputting From DB.

function my_bbcode($string)
{
$searches = array(
                ':)',
                ':D',
                ':(',
                ';)',
                ':P',
                '8)'
                );

$replaces = array(
                '<img src="bbcodeimgs/default/smile.gif" alt="" />',
                '<img src="bbcodeimgs/default/biggrin.gif" alt="" />',
                '<img src="bbcodeimgs/default/sad.gif" alt="" />',
                '<img src="bbcodeimgs/default/wink.gif" alt="" />',
                '<img src="bbcodeimgs/default/tongue.gif" alt="" />',
                '<img src="bbcodeimgs/default/cool.gif" alt="" />'
                );

$string = str_replace($searches, $replaces, $string);

$searches1 = array(
                '/\[b\](.*?)\[\/b\]/is',
                '/\[i\](.*?)\[\/i\]/is',
                '/\[u\](.*?)\[\/u\]/is',
                '/\[url\=(.*?)\](.*?)\[\/url\]/is',
                '/\[color\=(.*?)\](.*?)\[\/color\]/is'
                );
                
$replaces1 = array(
                '<strong>$1</strong>',
                '<em>$1</em>',
                '<u>$1</u>',
                '<a href="$1">$2</a>',
                '<span style="color: $1;">$2</span>'
                );
                
$string = preg_replace($searches1, $replaces1, $string);

return $string;

} 

sql

CREATE TABLE `site_bbcode` (
  `package` tinyint(4) NOT NULL,
  `search` varchar(255) NOT NULL,
  `replace` varchar(255) NOT NULL,
  `replace_type` varchar(10) NOT NULL,
  `disabled` tinyint(4) NOT NULL
);

Now that we have our DataBase Set up. Lets Connect!

config.php: (name it what you want 😛)

$db_local = 'localhost'; //Localhost tells the server that the DB is local (dah)
$db_user = 'da_user_man'; //This is the user to log into MySql.
$db_pass = 'never_give_out_your_pass!'; //This is the pass to log into MySql.
$db_name = 'site_bbcode'; //This is the DataBase we are chosing to work with.

//Connect to DataBase.
mysql_connect($db_local, $db_user, $db_pass)
    or die(mysql_error()); 

Ok now we are done with most db stuff...

Heres the hole bbcode php function.

require 'config.php';

function my_bbcode($string)
{
    //BBcode Out of database!!! FTW!!!!
    
    //Get BBcodes From DB
    $bbcode_package = 1;
    $bbcodes = mysql_query("SELECT `search`,`replace`,`replace_type` FROM `site_bbcode`
                           WHERE `package`='$bbcode_package'
                           AND `disabled`='0'");
        if(mysql_num_rows($bbcodes)!=0)
        {
        //Know if it is the first bbcode.
        $number['str'] = 1;
        $number['preg'] = 1;
        //Start array1
        $array1['str'] = "\$search['str'] = array(";
        $array1['preg'] = "\$search['preg'] = array(";
        //Start array2
        $array2['str'] = "\$replace['str'] = array(";
        $array2['preg'] = "\$replace['preg'] = array(";
            while($bbcode = mysql_fetch_array($bbcodes))
            {
                if($bbcode['replace_type'] == 'str')
                {
                //Make inner array
                    if($number['str'] != 1)
                    {
                    //Not first part (inner) array.
                    $bbcodeA = ", '{$bbcode['search']}'";
                    $bbcodeB = ", '{$bbcode['replace']}'";
                    }
                    else
                    {
                    //First part (inner) array.
                    $bbcodeA = "'{$bbcode['search']}'";
                    $bbcodeB = "'{$bbcode['replace']}'";
                    }
                $array1['str'] = $array1['str'].$bbcodeA;
                $array2['str'] = $array2['str'].$bbcodeB;
                //Not the first time any more.
                $number['str']++;
                }
                elseif($bbcode['replace_type'] == 'preg')
                {
                //Make inner array
                    if($number['preg'] != 1)
                    {
                    //First part (inner) array.
                    $bbcodeA = ", '{$bbcode['search']}'";
                    $bbcodeB = ", '{$bbcode['replace']}'";
                    }
                    else
                    {
                    //Not first part (inner) array.
                    $bbcodeA = "'{$bbcode['search']}'";
                    $bbcodeB = "'{$bbcode['replace']}'";
                    }
                $array1['preg'] = $array1['preg'].$bbcodeA;
                $array2['preg'] = $array2['preg'].$bbcodeB;
                //Not the first time any more.
                $number['preg']++;
                }
                else
                {
                //
                }
            }
        
        //Finish arrays.
        $array1['str'] = $array1['str'].");";
        $array2['str'] = $array2['str'].");";
        $array1['preg'] = $array1['preg'].");";
        $array2['preg'] = $array2['preg'].");";
        eval($array1['str']);
        eval($array2['str']);
        eval($array1['preg']);
        eval($array2['preg']);
        $string = str_replace($search['str'], $replace['str'], $string);
        $string = preg_replace($search['preg'], $replace['preg'], $string);
        }

        return $string;
} 

We need to break that up right? OK!

require 'config.php'; 

That just includes the mysql connection file we just made.

$bbcode_package = 1; 

This feature is optional. It just tells it to get bbcodes in package 1.

 $bbcodes = mysql_query("SELECT `search`,`replace`,`replace_type` FROM `site_bbcode`
                           WHERE `package`='$bbcode_package'
                           AND `disabled`='0'"); 

That is a sql query that selects the bbcodes from the DataBase. Again you can learn sql at the links above.

if(mysql_num_rows($bbcodes)!=0) 

This sees if there are any bbcodes.

 //No if it is the first bbcode.
        $number['str'] = 1;
        $number['preg'] = 1;
        //Start array1
        $array1['str'] = "\$search['str'] = array(";
        $array1['preg'] = "\$search['preg'] = array(";
        //Start array2
        $array2['str'] = "\$replace['str'] = array(";
        $array2['preg'] = "\$replace['preg'] = array("; 

We are now making arrays with strings in php! You will notice that there is $array['str'] and $array1['preg'].

This is because some codes (Regex) does not get replaced with str_replace right. It needs preg_replace. And it also goes the other way around. So this just keeps the arrays separate.

while($bbcode = mysql_fetch_array($bbcodes)) 

Start that loop!

if($bbcode['replace_type'] == 'str') 

This checks to see what replace type is needed.

if($number['str'] != 1)
{
//First part (inner) array.
$bbcodeA = ", '{$bbcode['search']}'";
$bbcodeB = ", '{$bbcode['replace']}'";
}
else
{
//Not first part (inner) array.
$bbcodeA = "'{$bbcode['search']}'";
$bbcodeB = "'{$bbcode['replace']}'";
} 

Start making that array!

$array1['preg'] = $array1['preg'].$bbcodeA;
$array2['preg'] = $array2['preg'].$bbcodeB; 

Put the vars to gether to form the array.

$number['str']++;
//And
$number['preg']++; 

This just adds + 1 onto it self making it not 1 anymore. This makes the array like:

array('', '', '', ''); 

Not:

array(, '', '', '', ''); 
elseif($bbcode['replace_type'] == 'preg') 

This is the same thing as:

if($bbcode['replace_type'] == 'str') 

Just with preg_replace not str_replace.

//Finish arrays.
$array1['str'] = $array1['str'].");";
$array2['str'] = $array2['str'].");";
$array1['preg'] = $array1['preg'].");";
$array2['preg'] = $array2['preg'].");"; 

This is what ends the arrays.

From:

array('bla', 'da', 'do', 'bla' 

To:

array('bla', 'da', 'do', 'bla'); 

The Coolest part!

eval($array1['str']);
eval($array2['str']);
eval($array1['preg']);
eval($array2['preg']); 

This makes the "string" arrays REAL arrays! Cool uh?

The last part! Replace!

 $string = str_replace($search['str'], $replace['str'], $string); 

Replace with str_replace.

  $string = preg_replace($search['preg'], $replace['preg'], $string); 

Replace with preg_replace.

return $string; 

Returns the string in it's new form!

BBCODES

INSERT INTO `site_bbcode` (`package`, `search`, `replace`, `replace_type`, `disabled`) VALUES
(1, ':)', '<img src="img_smile" alt="" />', 'str', 0),
(1, ':D', '<img src="img_bigSmile" alt="" />', 'str', 0),
(1, ':(', '<img src="img_sad" alt="" />', 'str', 0),
(1, ';)', '<img src="img_wink" alt="" />', 'str', 0),
(1, ':P', '<img src="img_tongue" alt="" />', 'str', 0),
(1, '8)', '<img src="img_cool" alt="" />', 'str', 0),
(1, '/\\[b\\](.*?)\\[\\/b\\]/is', '<strong>$1</strong>', 'preg', 0),
(1, '/\\[i\\](.*?)\\[\\/i\\]/is', '<em>$1</em>', 'preg', 0),
(1, '/\\[u\\](.*?)\\[\\/u\\]/is', '<u>$1</u>', 'preg', 0),
(1, '/\\[url\\=(.*?)\\](.*?)\\[\\/url\\]/is', '<a href="$1">$2</a>', 'preg', 0),
(1, '/\\[color\\=(.*?)\\](.*?)\\[\\/color\\]/is', '<span style="color: $1;">$2</span>', 'preg', 0),
(1, '/\\[highlight\\](.*?)\\[\\/highlight\\]/is', '<span class="thighlight">$1</span>', 'preg', 0);

Jacques jacques@gw-designs.co.za http://coding.biz.tm Come join and lets make it a place to learn all the noobies how to code


NEVER FORGET TO CLICK THE TANX BUTTON IF U LIKE WHAT IM SHARING OR HELPING WITH

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close