12

我有一个 phpBB2 论坛,其中的帖子存储在 BBCode 中。论坛帖子在数据库中的存储方式如下:

[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]

我正在寻找任何可以帮助我将此语法转换为 Markdown 的工具。理想情况下,我只想转换[b], [i], [quote], [url],[code][list]标签。最好将[img]标记转换为 Markdown 中的链接以避免页面大小调整问题。任何纯粹的表现元素,如[color][size]标签都会转换为纯文本。

4

1 回答 1

4

试试这个功能:

<?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);
?>

这应该可以完成您需要的所有替换:)

于 2012-01-11T12:28:16.870 回答