Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Utility/Token.php \Drupal\Core\Utility\Token::scan()
  2. 9 core/lib/Drupal/Core/Utility/Token.php \Drupal\Core\Utility\Token::scan()

Builds a list of all token-like patterns that appear in the text.

Parameters

string $text: The text to be scanned for possible tokens.

Return value

array An associative array of discovered tokens, grouped by type.

1 call to Token::scan()
Token::doReplace in core/lib/Drupal/Core/Utility/Token.php
Replaces all tokens in a given string with appropriate values.

File

core/lib/Drupal/Core/Utility/Token.php, line 291

Class

Token
Drupal placeholder/token replacement system.

Namespace

Drupal\Core\Utility

Code

public function scan($text) {
  if (!is_string($text)) {
    @trigger_error('Calling ' . __METHOD__ . '() with a $text parameter of type other than string is deprecated in drupal:10.1.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3334317', E_USER_DEPRECATED);
    $text = (string) $text;
  }

  // Matches tokens with the following pattern: [$type:$name]
  // $type and $name may not contain [ ] characters.
  // $type may not contain : or whitespace characters, but $name may.
  preg_match_all('/
      \\[             # [ - pattern start
      ([^\\s\\[\\]:]+)  # match $type not containing whitespace : [ or ]
      :              # : - separator
      ([^\\[\\]]+)     # match $name not containing [ or ]
      \\]             # ] - pattern end
      /x', $text, $matches);
  $types = $matches[1];
  $tokens = $matches[2];

  // Iterate through the matches, building an associative array containing
  // $tokens grouped by $types, pointing to the version of the token found in
  // the source text. For example, $results['node']['title'] = '[node:title]';
  $results = [];
  for ($i = 0; $i < count($tokens); $i++) {
    $results[$types[$i]][$tokens[$i]] = $matches[0][$i];
  }
  return $results;
}