php - Recursive BBCode Parsing -
i'm trying parse bbcode in script. now, works seamelessly, until try indent bbcode that's more bold or underline - such spoiler, url, font size, etc. - screws up. here's code:
function parse_bbcode($text) { global $db; $oldtext = $text; $bbcodes = $db->select('*', 'bbcodes'); foreach ($bbcodes $bbcode) { switch ($bbcode->type) { case 'simple': { $find = '{content}'; $replace = '${1}'; $text = preg_replace( '/\['.$bbcode->tag.'\](.+)\[\/'.$bbcode->tag.'\]/i', str_replace($find, $replace, $bbcode->html), $text); break; } case 'property': case 'options': { $find = array ( '{property}', '{content}' ); $replace = array ( '${1}', '${2}' ); $text = preg_replace( '/\['.$bbcode->tag.'\=(.[^\"]*)\](.+)\[\/'.$bbcode->tag.'\]/i', str_replace($find, $replace, $bbcode->html), $text); break; } } } return $text; }
now guess regex doesn't recursiveness in pattern. how can improve it? sample $bbcode object such:
stdclass::__set_state(array( 'id' => '2', 'name' => 'italic', 'type' => 'simple', 'tag' => 'i', 'button_image' => null, 'button_text' => '<i>i</i>', 'options' => '', 'prompt' => null, 'html' => '<i>{content}</i>', 'order' => '1', )) stdclass::__set_state(array( 'id' => '3', 'name' => 'url', 'type' => 'property', 'tag' => 'url', 'button_image' => null, 'button_text' => 'http://', 'options' => '', 'prompt' => 'url address', 'html' => '<a href="{property}">{content}</a>', 'order' => '4', ))
as gordon said in comments php has bbcode parser, no reason reinvent wheel.
the native parser pecl package though, have install it. if that's not option (for instance due shared hosting), there pear package: http://pear.php.net/package/html_bbcodeparser
in addition those, can take @ forums using bb code source code, , either use parser, or improve it. there several php implementations listed @ http://www.bbcode.org/implementations.php
Comments
Post a Comment