You are here

public static function XBBCodeParser::parseAttributes in Extensible BBCode 8.3

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

Parse a string of attribute assignments.

Parameters

string $argument: The string containing the attributes, including initial whitespace.

Return value

string[] An associative array of all attributes.

1 call to XBBCodeParser::parseAttributes()
TagElement::__construct in src/Parser/Tree/TagElement.php
TagElement constructor.

File

src/Parser/XBBCodeParser.php, line 124

Class

XBBCodeParser
The standard XBBCode parser.

Namespace

Drupal\xbbcode\Parser

Code

public static function parseAttributes($argument) : array {
  $assignments = [];
  preg_match_all("/\n    (?<=\\s)                                # preceded by whitespace.\n    (?'key'[\\w-]+)=\n    (?:\n        (?'quote'['\"]|&quot;|&\\#039;)     # quotes may be encoded.\n        (?'value'\n          (?:\\\\.|(?!\\\\|\\k'quote')[^\\\\])*   # value can contain the delimiter.\n        )\n        \\k'quote'\n        |\n        (?'unquoted'\n          (?!\\g'quote')           # unquoted values cannot start with a quote.\n          (?:\\\\.|[^\\s\\\\])*\n        )\n    )\n    (?=\\s|\$)/x", $argument, $assignments, PREG_SET_ORDER);
  $attributes = [];
  foreach ($assignments as $assignment) {

    // Strip backslashes from the escape sequences in each case.
    $value = $assignment['value'] ?: $assignment['unquoted'];
    $attributes[$assignment['key']] = stripslashes($value);
  }
  return $attributes;
}