2010-01-05 15 views
12

Ho un forum phpBB2 con post memorizzati in BBCode. I post del forum sono memorizzati in questo modo nel database:Conversione di post BBCode phpBB su 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] 

che sto cercando eventuali strumenti che potrebbero aiutare a convertire questa sintassi per Markdown. Idealmente, vorrei convertire solo i tag [b], [i], [quote], [url], [code] e [list]. Sarebbe preferibile convertire i tag [img] in collegamenti in Markdown per evitare problemi di ridimensionamento della pagina. Qualsiasi elemento puramente di presentazione come i tag sarebbe stato convertito in testo normale.

risposta

4

Prova questa funzione:

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

Questo dovrebbe fare tutte le sostituzioni necessari :)