Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Interpolator.php

function Interpolator::interpolate

Replaces tokens in a string with values from an associative array.

Tokens are surrounded by delimiters, e.g. square brackets "[key]". The characters that surround the key may be defined when the Interpolator is constructed.

Example: If the message is 'Hello, [user.name]', then the value of the user.name item is fetched from the array, and the token [user.name] is replaced with the result.

Parameters

string $message: Message containing tokens to be replaced.

array $extra: Data to use for interpolation in addition to whatever was provided to self::setData().

string|bool $default: (optional) The value to substitute for tokens that are not found in the data. If FALSE, then missing tokens are not replaced. Defaults to an empty string.

Return value

string The message after replacements have been made.

File

composer/Plugin/Scaffold/Interpolator.php, line 97

Class

Interpolator
Injects config values from an associative array into a string.

Namespace

Drupal\Composer\Plugin\Scaffold

Code

public function interpolate($message, array $extra = [], $default = '') {
    $data = $extra + $this->data;
    $replacements = $this->replacements($message, $data, $default);
    return strtr($message, $replacements);
}
RSS feed
Powered by Drupal