You are here

public static function XBBCodeParser::tokenize in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x src/Parser/XBBCodeParser.php \Drupal\xbbcode\Parser\XBBCodeParser::tokenize()

Find the opening and closing tags in a text.

Parameters

string $text: The source text.

array|\ArrayAccess|null $allowed: An array keyed by tag name, with non-empty values for allowed tags. Omit this argument to allow all tag names.

Return value

array[] The tokens.

1 call to XBBCodeParser::tokenize()
XBBCodeParser::parse in src/Parser/XBBCodeParser.php
Parse a text and build an element tree.

File

src/Parser/XBBCodeParser.php, line 59

Class

XBBCodeParser
The standard XBBCode parser.

Namespace

Drupal\xbbcode\Parser

Code

public static function tokenize($text, $allowed = NULL) : array {

  // Find all opening and closing tags in the text.
  $matches = [];
  preg_match_all("%\n      \\[\n        (?'closing'/?)\n        (?'name'[a-z0-9_-]+)\n        (?'argument'\n          (?:(?=\\k'closing')            # only take an argument in opening tags.\n            (?:\n              =(?:\\\\.|[^\\\\\\[\\]])*  # unquoted option must escape brackets.\n              |\n              =(?'quote1'['\"]|"|&\\#039;)\n               (?:\\\\.|(?!\\k'quote1')[^\\\\])*\n               \\k'quote1'\n              |\n              (?:\\s+[\\w-]+=\n                (?:\n                  (?'quote2'['\"]|"|&\\#039;)\n                  (?:\\\\.|(?!\\k'quote2')[^\\\\])*\n                  \\k'quote2'\n                  |\n                  (?!\\g'quote2')        # unquoted values cannot begin with quotes.\n                  (?:\\\\.|[^\\[\\]\\s\\\\])*\n                )\n              )*\n            )\n          )?\n        )\n      ]\n      %x", $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  $tokens = [];
  foreach ($matches as $i => $match) {
    $name = $match['name'][0];
    if ($allowed && empty($allowed[$name])) {
      continue;
    }
    $start = $match[0][1];
    $tokens[] = [
      'name' => $name,
      'start' => $start,
      'end' => $start + strlen($match[0][0]),
      'argument' => $match['argument'][0],
      'closing' => !empty($match['closing'][0]),
    ];
  }
  return $tokens;
}