$currentItem = null; $nodeChildren = $node->children; foreach ($nodeChildren as $child) { $validText = $child instanceof BBText && trim($child->text); // if the child is a list item, the following children (which are not list items) should be added to the child. if ($child instanceof BBTag && $child->tagName == '*') { $currentItem = $child; } else { if ($validText || !$child instanceof BBText) { if ($currentItem == null) { // this means the first component of the list is not a list item $currentItem = new BBTag('*'); array_unshift($node->children, $currentItem); $currentItem->parent = $node; } $node->remove($child); $currentItem->add($child); } } } return '<ul class="bblist">' . $handler->dumper->dumpChildren($node) . '</ul>'; } $parser->debug = true; // $str = '[b][u][/b]'; // $str = '[b][u][/b][/u]'; // $str = '[/a][b][/c][/b]'; $str = '[a][c][b][/c][/b][/a]'; $node = $parser->parse($str); print_r($node); // echo $dumper->dump($node); echo "\r\n";