BBCode Listen

versuch13

Erfahrenes Mitglied
Hey. Ich benutze schon länger diese Regulären Ausdrücke um Listen BBCode umzusetzen:

PHP:
    function bbcode2($text) {
        preg_match_all('/\
[ list/i', $text, $matches);
        $opentags = count($matches['0']);
        preg_match_all('/\[ \/list\]/i', $text, $matches);
        $unclosed = $opentags - count($matches['0']);
        for ($i = 0; $i < $unclosed; $i++) 
        $text .= '
[ /list]
';    
        
    // listen
    // begin processing for 
[list] and 
[*]
    if (stristr($text, '
[list') !== FALSE) { // prevent useless processing
      $l_type = array(
        NULL   => array('style' => 'circle', 'tag' => 'ul'),
        'c'    => array('style' => 'circle', 'tag' => 'ul'),
        'd'    => array('style' => 'disc', 'tag' => 'ul'),
        's'    => array('style' => 'square', 'tag' => 'ul'),
        '1'    => array('style' => 'decimal', 'tag' => 'ol'),
        'a'    => array('style' => 'lower-alpha', 'tag' => 'ol'),
        'A'    => array('style' => 'upper-alpha', 'tag' => 'ol'),
        'i'    => array('style' => 'lower-roman', 'tag' => 'ol'),
        'I'    => array('style' => 'upper-roman', 'tag' => 'ol')
        );
      $text = preg_replace('#(\[[/]*)list(.*?\])#si', "$1\x07$2", $text);
  
      // replace to <li> tags - 
[*]..
[*]|
[*]..
[ /list]

      $text = preg_replace('#\[\*(?::\w+)?\]([^\x07]*?)(?=\s*?(\[\*(?::\w+)?\]|\[/\x07(?::\w+)?\]))#si', '<li>$1</li>', $text);
      // add </li> tags to nested <li> - 
[ /list]
..
[ /list]

      $text = preg_replace('#(\[/\x07(?::\w+)?\])(?=[^\x07]*?\[/\x07(?::\w+)?\])#si', '$1</li>', $text);
      // add </li> tags to nested <li> - 
[ /list]
..
[*]..
[ list]
      $text = preg_replace('#(\[/\x07(?::\w+)?\])(?=[^\x07]*?\[\*(?::\w+)?\][^\x07]*?\[\x07.*(?::\w+)?\])#si', '$1</li>', $text);
      // replace to <li> tags for nested <li> - 
[*]..
[ list]
      $text = preg_replace('#\[\*(?::\w+)?\]([^\x07]*)?(?=\[\x07.*(?::\w+)?\])#si', '<li>$1', $text);
  
      // replace to <ol>/<ul> and </ol>/</ul> tags
      // It will be better to use &count and do-while, if php 5 or higher.
      while (preg_match("#\[\x07[=]*((?-i)[cds1aAiI])*(?::\w+)?\]([^\x07]*)\[/\x07(?::\w+)?\]#si", $text)) {
        $text = preg_replace("#\[\x07[=]*((?-i)[cds1aAiI])*(?::\w+)?\]([^\x07]*)\[/\x07(?::\w+)?\]#esi", '"<". $l_type[\'$1\']["tag"] ." class=\"bb-list\" style=\"list-style-type:". $l_type[\'$1\']["style"] .";\">". str_replace(\'\"\', \'"\', \'$2\') ."</". $l_type[\'$1\']["tag"] .">"', $text);
      }
  
      // remove <br /> tags
      $text = preg_replace('#(<(ul|ol|li).*>.*)<br />#i', '$1', $text);
      $text = preg_replace('#</li><br />#i', '</li>', $text);
    }
        
        return $text;
        
    }
    
    $str = "
[ list=decimal]
[*]foo
[ /list]
";
    
    echo bbcode2($str);


Das lief eigentlich auch immer. Jetzt habe ich aber irgendwie ein Problem damit und zwar wird mir als Ergebnis nun folgendes geliefert:

Code:
[=decimal]<li>foo</li>[/]

Kann mir da jemand behilflich sein?
 
Zurück