Results 1 to 3 of 3

Thread: BB -> HTML

  1. #1

    BB -> HTML

    is there a working framework or something that i can use to turn bb code into html?

  2. #2
    Join Date
    May 2009
    Location
    Tennessee
    Posts
    303
    bbcode implementation tends to differ among different applications. It's probably best to search for something specific to the application you're trying to generate html from.

    As a general starting point, here's some code I found that seems to do the trick most of the time:
    Code:
    $preg = array(
    '/(?<!\\\\)\[color(?::\w+)?=(.*?)\](.*?)\[\/color(?::\w+)?\]/si'   => "<span style=\"color:\\1\">\\2</span>",
    '/(?<!\\\\)\[size(?::\w+)?=(.*?)\](.*?)\[\/size(?::\w+)?\]/si'     => "<span style=\"font-size:\\1\">\\2</span>",
    '/(?<!\\\\)\[align(?::\w+)?=(.*?)\](.*?)\[\/align(?::\w+)?\]/si'   => "<div style=\"text-align:\\1\">\\2</div>",
    '/(?<!\\\\)\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si'                 => "<span style=\"font-weight:bold\">\\1</span>",
    '/(?<!\\\\)\[i(?::\w+)?\](.*?)\[\/i(?::\w+)?\]/si'                 => "<span style=\"font-style:italic\">\\1</span>",
    '/(?<!\\\\)\[u(?::\w+)?\](.*?)\[\/u(?::\w+)?\]/si'                 => "<span style=\"text-decoration:underline\">\\1</span>",
    '/(?<!\\\\)\[center(?::\w+)?\](.*?)\[\/center(?::\w+)?\]/si'       => "<div style=\"text-align:center\">\\1</div>",
    
    // [email]
    '/(?<!\\\\)\[email(?::\w+)?\](.*?)\[\/email(?::\w+)?\]/si'         => "<a href=\"mailto:\\1\" class=\"bb-email\">\\1</a>",
    '/(?<!\\\\)\[email(?::\w+)?=(.*?)\](.*?)\[\/email(?::\w+)?\]/si'   => "<a href=\"mailto:\\1\" class=\"bb-email\">\\2</a>",
    // [url]
    '/(?<!\\\\)\[url(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si'             => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>",
    '/(?<!\\\\)\[url(?::\w+)?=(.*?)?\](.*?)\[\/url(?::\w+)?\]/si'      => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\2</a>",
    // [img]
    '/(?<!\\\\)\[img(?::\w+)?\](.*?)\[\/img(?::\w+)?\]/si'             => "<img src=\"\\1\" alt=\"\\1\" class=\"bb-image\" />",
    '/(?<!\\\\)\[img(?::\w+)?=(.*?)\](.*?)\[\/img(?::\w+)?\]/si'             => "<img src=\"\\1\" alt=\"\\2\" class=\"bb-image\" />",
    
    // [list]
    '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\*(?::\w+)?\](.*?)(?=(?:\s*<br\s*\/?>\s*)?\[\*|(?:\s*<br\s*\/?>\s*)?\[\/?list)/si' => "<li class=\"bb-listitem\">\\1</li>",
    '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list(:(?!u|o)\w+)?\](?:<br\s*\/?>)?/si'    => "</ul>",
    '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:u(:\w+)?\](?:<br\s*\/?>)?/si'         => "</ul>",
    '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:o(:\w+)?\](?:<br\s*\/?>)?/si'         => "</ol>",
    '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(:(?!u|o)\w+)?\]\s*(?:<br\s*\/?>)?/si'   => "<ul class=\"bb-list-unordered\">",
    '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:u(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "<ul class=\"bb-list-unordered\">",
    '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:o(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "<ol class=\"bb-list-ordered\">",
    
    '/\\n/'																=> "<br />",
    '/\[br\]/'															=> "<br />",
    // escaped tags like \[b], \[color], \[url], ...
    '/\\\\(\[\/?\w+(?::\w+)*\])/'                                      => "\\1"
    );
    
    $t = preg_replace(array_keys($preg), array_values($preg), $text);
    AudioProbe.net - Playing the best music from the 70s, 80s, 90s, 00s, and more...
    Commercial free, high quality 192kb/s, and firewall friendly port 80!
    Look for us in the iTunes Radio directory under Adult Contemporary

  3. #3
    I'm using http://www.christian-seiler.de/proje.../index_en.html

    but a problem im getting when going from BBCode to HTML is im getting:

    Code:
    &lt;br /&gt;<br />
    and making the page look bad with all the <br />'s

    anyway to remove the one <br />'s that are showing?

Similar Threads

  1. Replies: 2
    Last Post: 09-17-2010, 12:49 PM
  2. Replies: 2
    Last Post: 03-25-2008, 05:26 AM
  3. Replies: 12
    Last Post: 05-27-2005, 02:51 PM
  4. Replies: 0
    Last Post: 10-15-2003, 07:34 PM
  5. sending HTML with mail() and Outlook receives HTML code
    By ladraum in forum Programming Discussion
    Replies: 1
    Last Post: 05-07-2003, 05:09 PM

Posting Permissions

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