You are here

protected function ShortcodeService::parseAttrs in Shortcode 8

Same name and namespace in other branches
  1. 2.0.x src/ShortcodeService.php \Drupal\shortcode\ShortcodeService::parseAttrs()

Retrieve all attributes from the Shortcodes tag.

The attributes list has the attribute name as the key and the value of the attribute as the value in the key/value pair. This allows for easier retrieval of the attributes, since all attributes have to be known.

Parameters

string $text: The Shortcode tag attribute line.

Return value

array List of attributes and their value.

1 call to ShortcodeService::parseAttrs()
ShortcodeService::processTag in src/ShortcodeService.php
Regular Expression callable for do_shortcode() for calling Shortcode hook.

File

src/ShortcodeService.php, line 454

Class

ShortcodeService
Provide the ShortCode service.

Namespace

Drupal\shortcode

Code

protected function parseAttrs($text) {
  $attributes = [];
  if (empty($text)) {
    return $attributes;
  }
  $pattern = '/(\\w+)\\s*=\\s*"([^"]*)"(?:\\s|$)|(\\w+)\\s*=\\s*\'([^\']*)\'(?:\\s|$)|(\\w+)\\s*=\\s*([^\\s\'"]+)(?:\\s|$)|"([^"]*)"(?:\\s|$)|(\\S+)(?:\\s|$)/';
  $text = preg_replace("/[\\x{00a0}\\x{200b}]+/u", " ", $text);
  $text = html_entity_decode($text);
  if (preg_match_all($pattern, $text, $match, PREG_SET_ORDER)) {
    foreach ($match as $m) {
      if (!empty($m[1])) {
        $attributes[strtolower($m[1])] = stripcslashes($m[2]);
      }
      elseif (!empty($m[3])) {
        $attributes[strtolower($m[3])] = stripcslashes($m[4]);
      }
      elseif (!empty($m[5])) {
        $attributes[strtolower($m[5])] = stripcslashes($m[6]);
      }
      elseif (isset($m[7]) and strlen($m[7])) {
        $attributes[] = stripcslashes($m[7]);
      }
      elseif (isset($m[8])) {
        $attributes[] = stripcslashes($m[8]);
      }
    }
  }
  else {
    $attributes = ltrim($text);
  }
  return $attributes;
}