You are here

function token_replace in Token 6

Same name and namespace in other branches
  1. 5 token.module \token_replace()

Replace all tokens in a given string with appropriate values.

Parameters

$text: A string potentially containing replaceable tokens.

$type: (optional) A flag indicating the class of substitution tokens to use. If an object is passed in the second param, 'type' should contain the object's type. For example, 'node', 'comment', or 'user'. If no type is specified, only 'global' site-wide substitution tokens are built.

$object: (optional) An object to use for building substitution values (e.g. a node comment, or user object).

$leading: (optional) Character(s) to prepend to the token key before searching for matches. Defaults to TOKEN_PREFIX.

$trailing: (optional) Character(s) to append to the token key before searching for matches. Defaults to TOKEN_SUFFIX.

$options: (optional) A keyed array of settings and flags to control the token generation and replacement process. Supported options are:

  • clear: A boolean flag indicating that tokens should be removed from the final text if no replacement value can be generated.

$flush: (optional) A flag indicating whether or not to flush the token cache. Useful for processes that need to slog through huge numbers of tokens in a single execution cycle. Flushing it will keep them from burning through memory. Defaults to FALSE.

Return value

Text with tokens replaced.

3 calls to token_replace()
TokenUnitTestCase::testClearOption in ./token.test
Test the $options['clear'] parameter for token_replace().
TokenUnitTestCase::testSystemTokenRecognition in ./token.test
Test whether token-replacement works in various contexts.
token_rules_input_evaluator_apply in ./token.rules.inc
Apply the input evaluator.

File

./token.module, line 214
The Token API module.

Code

function token_replace($text, $type = 'global', $object = NULL, $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
  return token_replace_multiple($text, array(
    $type => $object,
  ), $leading, $trailing, $options, $flush);
}