Hello all, I was wondering how I can use php to parse lists like in the example below where the [*] isn't closed. [list] [*] [*] [/list]
There are hundreds of frameworks for this... http://www.google.co.uk/search?hl=e...esnum=0&ct=result&cd=1&q=parse+bbcode&spell=1
Yea but I can't really find a simple source for this code or something that explains how it actually works. Would like to learn from it .
This is how vBulletin parses it... Im not sure what you will make of it... /** * Handles a [list] tag. Makes a bulleted or ordered list. * * @param string The body of the list. * @param string If tag has option, the type of list (ordered, etc). * * @return string HTML representation of the tag. */ function handle_bbcode_list($text, $type = '') { if ($type) { switch ($type) { case 'A': $listtype = 'upper-alpha'; break; case 'a': $listtype = 'lower-alpha'; break; case 'I': $listtype = 'upper-roman'; break; case 'i': $listtype = 'lower-roman'; break; case '1': //break missing intentionally default: $listtype = 'decimal'; break; } } else { $listtype = ''; } // emulates ltrim after nl2br $text = preg_replace('#^(\s|<br>|<br />)+#si', '', $text); $bullets = preg_split('#\s*\[\*\]#s', $text, -1, PREG_SPLIT_NO_EMPTY); if (empty($bullets)) { return "\n\n"; } $output = ''; foreach ($bullets AS $bullet) { $output .= $this->handle_bbcode_list_element($bullet); } if ($listtype) { return '<ol style="list-style-type: ' . $listtype . '">' . $output . '</ol>'; } else { return "<ul>$output</ul>"; } } PHP: