2010-01-05 26 views
12

Tôi có một diễn đàn phpBB2 với các bài đăng được lưu trữ trong BBCode. Các bài đăng trên diễn đàn được lưu trữ như thế này trong cơ sở dữ liệu:Chuyển đổi bài đăng phpBB BBCode thành Markdown

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

Tôi sau bất kỳ công cụ nào có thể giúp tôi chuyển đổi cú pháp này thành Đánh dấu. Lý tưởng nhất là tôi chỉ muốn chuyển đổi các thẻ [b], [i], [quote], [url], [code][list]. Sẽ thích hợp hơn khi chuyển đổi các thẻ [img] thành các liên kết trong Đánh dấu để tránh các vấn đề về thay đổi kích thước trang. Bất kỳ yếu tố hoàn toàn thuyết trình nào như các thẻ [color][size] sẽ được chuyển thành văn bản thuần túy.

Trả lời

4

Hãy thử chức năng này:

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

này nên làm tất cả những thay thế bạn cần :)

Các vấn đề liên quan