UBB Code Conversion

Discussion in 'PHP' started by cancer10, Jun 26, 2008.

  1. #1
    Hi,

    I am trying to create a forum within which I have a "Post a Thread" page which has a simple HTML textarea element. I also have those BOLD and Itallic buttons that would wrap your text with UBB code when you click on them.

    As you know there are many UBB codes like , ,
     
    cancer10, Jun 26, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    I wrote 3 forum software, and I suggest as much as you save processing, you make the forum faster.

    The very first things is, I will suggest you not convert bbcode to html while saving, rather do the parsing when displaying.

    The bb code function you are using may be more resources consuming when large list of bbcodes added.

    Something like this;
    $search = array(
      '#\[url]([a-z]+?://){1}(.*?)\[/url]#', 
      '#\[url](.*?)\[/url\]#', 
      '#\[url=([a-z]+?://){1}(.*?)\](.*?)\[/url]#', 
      '#\[b](.*?)\[/b\]#', 
      '#\[i](.*?)\[/i\]#', 
      '#\[u](.*?)\[/u\]#', 
      '#\[img]([a-z]+?://){1}(.*?)\[/img]#', 
      '#\[img](.*?)\[/img\]#'
    ) ; 
    
    $replace = array(
      '<a href="\1\2" target=_blank>$1$2</a>', 
      '<a href=http://\1\2 target=_blank>\1</a>', 
      '<a href="\1\2" target=_blank>\3</a>', 
      '<b>\1</b>', 
      '<i>\1</i>', 
      '<u>\1</u>', 
      '<img src="\1\2">', 
      '<img src="\1\2" />'
    ) ; 
    
    $text = preg_replace($search, $replace, $text); 
    PHP:

    I hope it helps.

    regards
     
    Vooler, Jun 26, 2008 IP
  3. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanx Vooler


    Can you please post a complete list of UBB codes and their replacement so that I dont miss any other?

    Also, i m a newbie in regular expression so it would help me if you post it.


    Thanx
     
    cancer10, Jun 26, 2008 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    '[quote]'
    '[code]'
    '[size]'
    '[color]'
    PHP:
    Some forums do have '' too.

    regards
     
    Vooler, Jun 26, 2008 IP
  5. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #5
    Reply to your private message, as I noticed you cant receive private message, so here you go.

    LiteBoards.com was the site used to be a site for free forum software. But is abandoned now, but I can send you software zip. this was my first forum software using plain files not database. Then I wrote LitBoard Laser with MySQL, but is property of clixint technologies and they use on their forums, I cant disclose its source. The third one I wrote was Flash based, and probably you dont need that one. Please let me know in the thread what you need.

    regards
     
    Vooler, Jun 26, 2008 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Don't change to HTML when inserting. If you do, you will mix HTML and BBCODE.

    For example, if someone quotes a BBCODE lesson, the above won't work.

    Just parse the BBCODE when displaying it (VB/PHPBB/etc..) all do it that way.

    Here is my BBCode function:

    
    // BBCode Format
    function bbcode_format ($comment_body) {
    	$comment_body = htmlentities($comment_body);
    	$simple_search = array(
                    '/\[b\](.*?)\[\/b\]/is', // Bold
                    '/\[i\](.*?)\[\/i\]/is', // Italic
                    '/\[u\](.*?)\[\/u\]/is', // Underline
                    '/\[url\=(.*?)\](.*?)\[\/url\]/is', // URL + Title
                    '/\[url\](.*?)\[\/url\]/is', // URL
                    '/\[align\=(left|center|right)\](.*?)\[\/align\]/is', // Alignment
                    '/\[img\](.*?)\[\/img\]/is', // Image
                    '/\[mail\=(.*?)\](.*?)\[\/mail\]/is', // MailTo + Title
                    '/\[mail\](.*?)\[\/mail\]/is', // MailTo
                    '/\[font\=(.*?)\](.*?)\[\/font\]/is', // Font
                    '/\[size\=(.*?)\](.*?)\[\/size\]/is', // Size
                    '/\[color\=(.*?)\](.*?)\[\/color\]/is', // Color
    	);
    
    	$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
    	$comment_body = preg_replace ($simple_search, $simple_replace, $comment_body);
    
    	// Do <blockquote> BBCode
    	$comment_body = bbcode_quote ($comment_body);
    
    	return $comment_body;
    }
    
    // BBCode Quote Format
    function bbcode_quote ($comment_body) {
    	$open = '<blockquote>';
    	$close = '</blockquote>';
    
    	// How often is the open tag?
    	preg_match_all ('/\[quote\]/i', $comment_body, $matches);
    	$opentags = count($matches['0']);
    
    	// How often is the close tag?
    	preg_match_all ('/\[\/quote\]/i', $comment_body, $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++) {
    		$comment_body .= '</blockquote>';
    	}
    
    	// Do replacement
    	$comment_body = str_replace ('[' . 'quote]', $open, $comment_body);
    	$comment_body = str_replace ('[/' . 'quote]', $close, $comment_body);
    
    	return $comment_body;
    }
    
    PHP:
    Peace,
     
    Barti1987, Jun 26, 2008 IP
  7. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Was wondering if u could write me a function in php that would convert the following to HTML and another function that would convert html to ubbc?


    To avoid problems, I have replaced [ with <

    
    <b]</b]
    <i]</i]
    <u]</u]
    <s]</s]
    <glow=red,2,300]</glow]
    <shadow=red,left,300]</shadow]
    <move]</move]
    <pre]</pre]
    <left]</left]
    <center]</center]
    <right]</right]
    <hr]
    <size=2]</size]
    <font=Verdana]</font]
    <blockquote]</blockquote]
    <url]</url]
    <ftp]</ftp]
    <img]</img]
    <email]</email]
    <table]<tr]<td]</td]</tr]</table]
    <tr]</tr]
    <td]</td]
    <sup]</sup]
    <sub]</sub]
    <tt]</tt]
    <code]</code]
    <quote]</quote]
    <list]
    <*]
    <*]
    <*]
    </list]
    
    
    Code (markup):
     
    cancer10, Jun 29, 2008 IP
  8. alvas

    alvas Peon

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you might want to check this http://phpclasses.sgboards.com/browse/package/951.html
     
    alvas, Jun 29, 2008 IP
    cancer10 likes this.
  9. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Alvas,

    Thanx for this wonderful link. You rock man. Reps added :)
     
    cancer10, Jun 29, 2008 IP
  10. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10

    HI

    First of all thanx for your code.


    Was wondering if you could add the following in your ubb code, then it would really be helpful for me.


    http://cupidsystems.com/ubbc_replacement.txt



    Thanx so much
     
    cancer10, Jul 5, 2008 IP