Author Topic: bbcode help  (Read 982 times)

Offline PROMO

  • Level 6
  • *
  • Posts: 22
  • Reputation: +0/-0
    • View Profile
bbcode help
« on: February 12, 2008, 05:38:17 PM »
i wanted to add bbcode into a text area. so that
when they can send pms using bbcode and also use bbcode in there profile signature.

how would i go about doing that?
is there a already made bbcode?

Offline leZourite

  • Level 12
  • *
  • Posts: 81
  • Reputation: +2/-0
    • View Profile
    • After doomsday
Re: bbcode help
« Reply #1 on: February 13, 2008, 12:17:45 AM »
Here is a simple BBcode format function, it does a little sanitization but you may have to sanitize before sending the text to the function anyway.

It's pretty straightforward : echo  bbcode_format ("blah blah text from input");

Code: [Select]
function bbcode_format ($str) {
   $str = htmlentities($str);

    $simple_search = array(
                '/\[b\](.*?)\[\/b\]/is',                               
                '/\[i\](.*?)\[\/i\]/is',                               
                '/\[u\](.*?)\[\/u\]/is',                               
                '/\[url\=(.*?)\](.*?)\[\/url\]/is',                         
                '/\[url\](.*?)\[\/url\]/is',                             
                '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',   
                '/\[img\](.*?)\[\/img\]/is',                           
                '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',                   
                '/\[mail\](.*?)\[\/mail\]/is',                           
                '/\[font\=(.*?)\](.*?)\[\/font\]/is',                   
                '/\[size\=(.*?)\](.*?)\[\/size\]/is',                   
                '/\[color\=(.*?)\](.*?)\[\/color\]/is',       
                );

    $simple_replace = array(
                '<strong>$1</strong>',
                '<em>$1</em>',
                '<u>$1</u>',
                '<a href="$1">$2</a>',
                '<a href="$1">$1</a>',
                '<div style="text-align: $1;">$2</div>',
                '<img src="$1" />',
                '<a href="mailto:$1">$2</a>',
                '<a href="mailto:$1">$1</a>',
                '<span style="font-family: $1;">$2</span>',
                '<span style="font-size: $1;">$2</span>',
                '<span style="color: $1;">$2</span>',
                );

    // Do simple BBCode's
    $str = preg_replace ($simple_search, $simple_replace, $str);

    // Do <blockquote> BBCode
    $str = bbcode_quote ($str);

    return $str;
}
function bbcode_quote ($str) {
    $open = '<blockquote>';
    $close = '</blockquote>';

    // How often is the open tag?
    preg_match_all ('/\[quote\]/i', $str, $matches);
    $opentags = count($matches['0']);

    // How often is the close tag?
    preg_match_all ('/\[\/quote\]/i', $str, $matches);
    $closetags = count($matches['0']);

    // Check how many tags have been unclosed
    // And add the unclosing tag at the end of the message
    $unclosed = $opentags - $closetags;
    for ($i = 0; $i < $unclosed; $i++) {
        $str .= '</blockquote>';
    }

    // Do replacement
    $str = str_replace ('[' . 'quote]', $open, $str);
    $str = str_replace ('[/' . 'quote]', $close, $str);

    return $str;
}

Offline PROMO

  • Level 6
  • *
  • Posts: 22
  • Reputation: +0/-0
    • View Profile
Re: bbcode help
« Reply #2 on: February 13, 2008, 01:34:30 AM »
i put that code in my classes.php right?

then when i have a textarea form i would put the echo bbcode_format?

Offline leZourite

  • Level 12
  • *
  • Posts: 81
  • Reputation: +2/-0
    • View Profile
    • After doomsday
Re: bbcode help
« Reply #3 on: February 13, 2008, 02:11:02 AM »
Oh well, yep you could i think, i dunno how your classes.php is build but if it is loaded with each pages, i guess so :)

then if you put in in a class, you will have to call it this way :

echo $instanceofclass->bbcode_format('text that need to be parsed'); //$instance of class is the $variable object where you assigned the class in the first place

Note, that the function (as you can see it's 2 functions linked) need to be referenced correctly if it is in a class, or else it won't work, so i changed the former code a little so it will work inside a class

Code: [Select]
public function bbcode_format ($str) {
   $str = htmlentities($str);

    $simple_search = array(
                '/\[b\](.*?)\[\/b\]/is',                               
                '/\[i\](.*?)\[\/i\]/is',                               
                '/\[u\](.*?)\[\/u\]/is',                               
                '/\[url\=(.*?)\](.*?)\[\/url\]/is',                         
                '/\[url\](.*?)\[\/url\]/is',                             
                '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',   
                '/\[img\](.*?)\[\/img\]/is',                           
                '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',                   
                '/\[mail\](.*?)\[\/mail\]/is',                           
                '/\[font\=(.*?)\](.*?)\[\/font\]/is',                   
                '/\[size\=(.*?)\](.*?)\[\/size\]/is',                   
                '/\[color\=(.*?)\](.*?)\[\/color\]/is',       
                );

    $simple_replace = array(
                '<strong>$1</strong>',
                '<em>$1</em>',
                '<u>$1</u>',
                '<a href="$1">$2</a>',
                '<a href="$1">$1</a>',
                '<div style="text-align: $1;">$2</div>',
                '<img src="$1" />',
                '<a href="mailto:$1">$2</a>',
                '<a href="mailto:$1">$1</a>',
                '<span style="font-family: $1;">$2</span>',
                '<span style="font-size: $1;">$2</span>',
                '<span style="color: $1;">$2</span>',
                );

    // Do simple BBCode's
    $str = preg_replace ($simple_search, $simple_replace, $str);

    // Do <blockquote> BBCode
    $str =$this->bbcode_quote ($str);

    return $str;
}

private function bbcode_quote ($str) {
    $open = '<blockquote>';
    $close = '</blockquote>';

    // How often is the open tag?
    preg_match_all ('/\[quote\]/i', $str, $matches);
    $opentags = count($matches['0']);

    // How often is the close tag?
    preg_match_all ('/\[\/quote\]/i', $str, $matches);
    $closetags = count($matches['0']);

    // Check how many tags have been unclosed
    // And add the unclosing tag at the end of the message
    $unclosed = $opentags - $closetags;
    for ($i = 0; $i < $unclosed; $i++) {
        $str .= '</blockquote>';
    }

    // Do replacement
    $str = str_replace ('[' . 'quote]', $open, $str);
    $str = str_replace ('[/' . 'quote]', $close, $str);

    return $str;
}



Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: bbcode help
« Reply #4 on: February 13, 2008, 11:03:59 AM »
PHP also has its own bbcode functions :)

http://nl2.php.net/manual/en/function.bbcode-create.php

Offline leZourite

  • Level 12
  • *
  • Posts: 81
  • Reputation: +2/-0
    • View Profile
    • After doomsday
Re: bbcode help
« Reply #5 on: February 13, 2008, 04:40:50 PM »
oh nice find, it looks like its an extension not in the default set of PHP though, so it depends of the host, or if you have a VPS or dedicated server

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal