function Token::findWithPrefix

Same name in other branches
  1. 9 core/lib/Drupal/Core/Utility/Token.php \Drupal\Core\Utility\Token::findWithPrefix()
  2. 10 core/lib/Drupal/Core/Utility/Token.php \Drupal\Core\Utility\Token::findWithPrefix()
  3. 11.x core/lib/Drupal/Core/Utility/Token.php \Drupal\Core\Utility\Token::findWithPrefix()

Returns a list of tokens that begin with a specific prefix.

Used to extract a group of 'chained' tokens (such as [node:author:name]) from the full list of tokens found in text. For example:

$data = array(
    'author:name' => '[node:author:name]',
    'title' => '[node:title]',
    'created' => '[node:created]',
);
$results = Token::findWithPrefix($data, 'author');
$results == array(
    'name' => '[node:author:name]',
);

Parameters

array $tokens: A keyed array of tokens, and their original raw form in the source text.

string $prefix: A textual string to be matched at the beginning of the token.

string $delimiter: (optional) A string containing the character that separates the prefix from the rest of the token. Defaults to ':'.

Return value

array An associative array of discovered tokens, with the prefix and delimiter stripped from the key.

File

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

Class

Token
Drupal placeholder/token replacement system.

Namespace

Drupal\Core\Utility

Code

public function findWithPrefix(array $tokens, $prefix, $delimiter = ':') {
    $results = [];
    foreach ($tokens as $token => $raw) {
        $parts = explode($delimiter, $token, 2);
        if (count($parts) == 2 && $parts[0] == $prefix) {
            $results[$parts[1]] = $raw;
        }
    }
    return $results;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.