我有一个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]
标记都会转换为纯文本。
答案 0 :(得分: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);
?>
这应该做你需要的所有替换:)