You are here

function token_update_token_text in Token 8

Same name and namespace in other branches
  1. 7 token.install \token_update_token_text()

Update a string containing Drupal 6 style tokens to Drupal 7 style tokens.

Parameters

$text: A string containing tokens.

$updates: An optional array of Drupal 7 tokens keyed by their Drupal 6 token name. The default tokens will be merged into this array. Note neither the old or new token names should include the surrounding bracket ([ and ]) characters.

Return value

A string with the tokens upgraded

See also

_token_upgrade_token_list()

File

./token.install, line 201
Install, update and uninstall functions for the token module.

Code

function token_update_token_text($text, $updates = [], $leading = '[', $trailing = ']') {
  $updates += _token_upgrade_token_list();
  $regex = '/' . preg_quote($leading, '/') . '([^\\s]*)' . preg_quote($trailing, '/') . '/';
  preg_match_all($regex, $text, $matches);
  foreach ($matches[1] as $old_token) {
    if (isset($updates[$old_token])) {
      $new_token = $updates[$old_token];
      $text = str_replace("{$leading}{$old_token}{$trailing}", "[{$new_token}]", $text);

      // Also replace any tokens that have a -raw suffix.
      $text = str_replace("{$leading}{$old_token}-raw{$trailing}", "[{$new_token}]", $text);
    }
  }
  return $text;
}