Converting phpBB BBCode posts to Markdown
Asked Answered
G

1

12

I have a phpBB2 forum with posts stored in BBCode. The forum posts are stored like this in the database:

[quote:e5adceb8e8][quote:e5adceb8e8="Person 2"][quote:e5adceb8e8="Person 3"]Nested quote[/quote:e5adceb8e8]Another nested quote[/quote:e5adceb8e8]Some text[/quote:e5adceb8e8]

[b:e5adceb8e8]Some bold text[/b:e5adceb8e8]
[i:e5adceb8e8]italic text[/i:e5adceb8e8]
[u:e5adceb8e8]underlined text[/u:e5adceb8e8]

[code:1:e5adceb8e8]print ("hello world!");[/code:1:e5adceb8e8]

[img:e5adceb8e8]http://www.google.co.nz/intl/en_com/images/logo_plain.png[/img:e5adceb8e8]

[url]http://google.com[/url]

[url=http://google.com]Google[/url]

[color=darkred:e5adceb8e8]
Coloured text[/color:e5adceb8e8]

[size=18:e5adceb8e8]
Big text[/size:e5adceb8e8]

[list:e5adceb8e8]
List Item 1
List Item 2
[/list:u:e5adceb8e8]

[list:e5adceb8e8]
[*:e5adceb8e8]List Item 1
[*:e5adceb8e8]List Item 2
[/list:u:e5adceb8e8]

[list=1:e5adceb8e8]
[*:e5adceb8e8]List Item 1
[*:e5adceb8e8]List Item 2
[/list:o:e5adceb8e8]

[list=a:e5adceb8e8]
[*:e5adceb8e8]List Item 1
[*:e5adceb8e8]List Item 2
[/list:o:e5adceb8e8]

I'm after any tools that could help me convert this syntax to Markdown. Ideally I'd only want to convert the [b], [i], [quote], [url], [code], and [list] tags. It would be preferable to convert the [img] tags to links in Markdown to avoid page resizing problems. Any purely presentational elements such as the [color] and [size] tags would converted to plain text.

Goya answered 5/1, 2010 at 10:57 Comment(0)
B
4

Try this function:

<?php
function bbcode ($bbcode) {
    $matches = array();
    if(preg_match_all ( "/\[list.*\].*\[\/list.*\]/Ui", $bbcode, $matches ) > 0)
    {
        $matches = $matches[0];
        foreach($matches as $match)
        {
            $replace = $match;

            $replace = preg_replace('/\[list:.*\](.*)\[\/list:.*\]/Ui', '<ul>\1</ul>', $replace);
            $replace = preg_replace('/\[list=1:.*\](.*)\[\/list:.*\]/Ui', '<ol>\1</ol>', $replace);
            $replace = preg_replace('/\[list=a:.*\](.*)\[\/list:.*\]/Ui', '<ol type="a">\1</ol>', $replace);
            $replace = preg_replace('/\[\*.*\](.*)<br>/Ui', '<li>\1</li>', $replace);
            $replace = preg_replace('/<br>/Ui', '</li><li>', $replace);
            $replace = preg_replace('/l\>\<\/li\>/Ui', 'l>', $replace);
            $replace = preg_replace('/\<li\>\<\//Ui', '</', $replace);
            $replace = str_replace('<li><li>', '<li>', $replace);

            $bbcode = str_replace($match, $replace, $bbcode);
        }
    }

    $search = array (
        "/\n/", //newlines
        "/\[b:.*\](.*)\[\/b:.*\]/Ui", //bold
        "/\[i:.*\](.*)\[\/i:.*\]/Ui", //italic
        "/\[u:.*\](.*)\[\/u:.*\]/Ui", //underline
        "/\[img:.*\](.*)\[\/img:.*\]/Ui", //images
        "/\[code:.*\](.*)\[\/code:.*\]/Ui", //code-blocks
        "/\[url\](.*)\[\/url\]/Ui", //links
        "/\[url=(.*)\](.*)\[\/url\]/Ui", //links with names
        "/\[color.*\](.*)\[\/color.*\]/Ui", //color
        "/\[size.*\](.*)\[\/size.*\]/Ui", //color
        "/\[quote:[^\]=]*\](.*)\[\/quote[^\]]*\]/i",
        "/\[quote:[^\]]*=\"([^\"]*)\"\](.*)\[\/quote[^\]]*\]/i",
    );
    $replace = array (
        '<br>',
        '<b>\1</b>',
        '<i>\1</i>',
        '<u>\1</u>',
        '<a href="\1">\1</a>', //images to links
        '<pre>\1</pre>',
        '<a href="\1">\1</a>',
        '<a href="\1">\2</a>',
        '\1',
        '\1',
        '<blockquote style="border:1px solid black;">\1</blockquote>',
        '<blockquote style="border:1px solid black;">\1:<br> \2</blockquote>',
    );
    $count = 0;
    $bbcode = preg_replace($search, $replace, $bbcode, -1, $count);
    if($count == 0) return $bbcode;
    else return bbcode($bbcode);
}

echo bbcode($bbcode);
?>

This should do all the replacements you need :)

Bronchoscope answered 11/1, 2012 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.