+ Reply to Thread
Results 1 to 6 of 6

Thread: BBcode using a DB.

  1. #1
    Moderator riderz is on a distinguished road riderz's Avatar
    Join Date
    Mar 2009
    Location
    south africa
    Posts
    1,259
    Thanks
    75
    Thanked 171 Times in 77 Posts
    Rep Power
    3

    Default BBcode using a DB.

    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.

    Code:
    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
    Code:
    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 :P)
    Code:
    $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.
    Code:
    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!
    Code:
    require 'config.php';
    
    That just includes the mysql connection file we just made.
    Code:
    $bbcode_package = 1;
    
    This feature is optional. It just tells it to get bbcodes in package 1.
    Code:
     $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.
    Code:
    if(mysql_num_rows($bbcodes)!=0)
    
    This sees if there are any bbcodes.
    Code:
     //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.
    Code:
    while($bbcode = mysql_fetch_array($bbcodes))
    
    Start that loop!
    Code:
    if($bbcode['replace_type'] == 'str')
    
    This checks to see what replace type is needed.
    Code:
    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!
    Code:
    $array1['preg'] = $array1['preg'].$bbcodeA;
    $array2['preg'] = $array2['preg'].$bbcodeB;
    
    Put the vars to gether to form the array.
    Code:
    $number['str']++;
    //And
    $number['preg']++;
    
    This just adds + 1 onto it self making it not 1 anymore.
    This makes the array like:
    Code:
    array('', '', '', '');
    
    Not:
    Code:
    array(, '', '', '', '');
    
    Code:
    elseif($bbcode['replace_type'] == 'preg')
    
    This is the same thing as:
    Code:
    if($bbcode['replace_type'] == 'str')
    
    Just with preg_replace not str_replace.
    Code:
    //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:
    Code:
    array('bla', 'da', 'do', 'bla'
    
    To:
    Code:
    array('bla', 'da', 'do', 'bla');
    
    The Coolest part!
    Code:
    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!
    Code:
     $string = str_replace($search['str'], $replace['str'], $string);
    
    Replace with str_replace.
    Code:
      $string = preg_replace($search['preg'], $replace['preg'], $string);
    
    Replace with preg_replace.
    Code:
    return $string;
    
    Returns the string in it's new form!

    BBCODES
    Code:
    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);
    
    I'm working on my six-pack...I've got 2 cans left.

    _________________
    Cheap Host Waplive Webhosting
    Jacques


    __________________

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

  2. #2
    Senior Member kei_ki7 is on a distinguished road kei_ki7's Avatar
    Join Date
    Jun 2009
    Location
    Philippines
    Posts
    309
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Rep Power
    2

    Default

    .. no offence, but i think you need to give some credits to the original poster.
    Earn Extra Cash ONLINE !!


    Make Money Online
    Register here for FREE:






  3. #3
    Moderator riderz is on a distinguished road riderz's Avatar
    Join Date
    Mar 2009
    Location
    south africa
    Posts
    1,259
    Thanks
    75
    Thanked 171 Times in 77 Posts
    Rep Power
    3

    Default

    well i just got this from other site im not taking credit of this its not my code and the coder not here so how to tanx him lol but hey im gratefull for all the coders on the wap
    I'm working on my six-pack...I've got 2 cans left.

    _________________
    Cheap Host Waplive Webhosting
    Jacques


    __________________

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

  4. #4
    Senior Member kei_ki7 is on a distinguished road kei_ki7's Avatar
    Join Date
    Jun 2009
    Location
    Philippines
    Posts
    309
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Rep Power
    2

    Default

    .. no im not telling that you taking credit with, a simple link back can with the name of the poster thats what i mean
    Earn Extra Cash ONLINE !!


    Make Money Online
    Register here for FREE:






  5. #5
    Super Moderator metulj metulj's Avatar
    Join Date
    Jan 2007
    Location
    your root... now think fast... =]
    Posts
    1,066
    Thanks
    96
    Thanked 35 Times in 26 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by kei_ki7
    .. no im not telling that you taking credit with, a simple link back can with the name of the poster thats what i mean
    at most.. simple posted(or coded) by blabla
    would be more than enough...
    theres no need for any link...
    as that would bring a lot of spam here...
    which i dont think it'll be allowed

    XHTML, PHP, MySQL, CSS & more


  6. #6
    Moderator riderz is on a distinguished road riderz's Avatar
    Join Date
    Mar 2009
    Location
    south africa
    Posts
    1,259
    Thanks
    75
    Thanked 171 Times in 77 Posts
    Rep Power
    3

    Default

    yea im always just sharing what i come up on the web and then i wont be going there again coz i dnt save pages lol
    I'm working on my six-pack...I've got 2 cans left.

    _________________
    Cheap Host Waplive Webhosting
    Jacques


    __________________

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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. bbcode stressed
    By Leviathan73 in forum Coding Forum
    Replies: 7
    Last Post: 05-10-09, 11:31
  2. New Bbcode For Lavalair
    By whiteboy544 in forum Coding Forum
    Replies: 20
    Last Post: 06-01-09, 20:15
  3. Bbcode Colors
    By sweetangel in forum Coding Forum
    Replies: 14
    Last Post: 15-09-08, 12:39
  4. Bbcode At Wapdesire
    By sweetangel in forum Coding Forum
    Replies: 11
    Last Post: 14-06-08, 17:35
  5. Bbcode At Staff Nicknames
    By sweetangel in forum Coding Forum
    Replies: 1
    Last Post: 01-03-08, 13:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO