You are here

function _xbbcode_list_tree in Extensible BBCode 7

Same name and namespace in other branches
  1. 8 xbbcode_list/xbbcode_list.module \_xbbcode_list_tree()
1 call to _xbbcode_list_tree()
_xbbcode_list_render in xbbcode_list/xbbcode_list.module

File

xbbcode_list/xbbcode_list.module, line 35

Code

function _xbbcode_list_tree($text) {
  $text = str_replace("\n", '', $text);
  $tokens = preg_split('/\\s*\\[(\\*+)\\]\\s*/', trim($text), -1, PREG_SPLIT_DELIM_CAPTURE);
  array_shift($tokens);
  $root = (object) array(
    'sub' => array(),
    'data' => NULL,
  );
  $stack = array(
    $root,
  );
  if ($tokens[0] != '*') {
    return NULL;
  }
  for ($i = 0; $i < count($tokens); $i += 2) {
    if (strlen($tokens[$i] > count($stack) + 1)) {
      return NULL;
    }
    elseif (strlen($tokens[$i]) == count($stack) + 1) {
      array_push($stack, end(end($stack)->sub));
    }
    else {
      while (strlen($tokens[$i]) < count($stack)) {
        array_pop($stack);
      }
    }
    end($stack)->sub[] = (object) array(
      'sub' => array(),
      'data' => $tokens[$i + 1],
    );
  }
  return $root;
}