| 7 token.inc | token_replace($text, array $data = array(), array $options = array()) |
| 8 token.inc | token_replace($text, array $data = array(), array $options = array()) |
Replaces all tokens in a given string with appropriate values.
Parameters
$text: A string potentially containing replaceable tokens.
$data: (optional) An array of keyed objects. For simple replacement scenarios 'node', 'user', and others are common keys, with an accompanying node or user object being the value. Some token types, like 'site', do not require any explicit information from $data and can be replaced even if it is empty.
$options: (optional) A keyed array of settings and flags to control the token replacement process. Supported options are:
- language: A language object to be used when generating locale-sensitive tokens.
- callback: A callback function that will be used to post-process the array of token replacements after they are generated. For example, a module using tokens in a text-only email might provide a callback to strip HTML entities from token values before they are inserted into the final text.
- clear: A boolean flag indicating that tokens should be removed from the final text if no replacement value can be generated.
- sanitize: A boolean flag indicating that tokens should be sanitized for display to a web browser. Defaults to TRUE. Developers who set this option to FALSE assume responsibility for running filter_xss(), check_plain() or other appropriate scrubbing functions before displaying data to users.
Return value
Text with tokens replaced.
18 calls to token_replace()
File
- includes/
token.inc, line 79 - Drupal placeholder/token replacement system.
Code
function token_replace($text, array $data = array(), array $options = array()) {
$text_tokens = token_scan($text);
if (empty($text_tokens)) {
return $text;
}
$replacements = array();
foreach ($text_tokens as $type => $tokens) {
$replacements += token_generate($type, $tokens, $data, $options);
if (!empty($options['clear'])) {
$replacements += array_fill_keys($tokens, '');
}
}
// Optionally alter the list of replacement values.
if (!empty($options['callback']) && function_exists($options['callback'])) {
$function = $options['callback'];
$function($replacements, $data, $options);
}
$tokens = array_keys($replacements);
$values = array_values($replacements);
return str_replace($tokens, $values, $text);
}
Login or register to post comments
Comments
token_replace('[user:picture]
token_replace('[user:picture]', array('user' => $user)); works
token_replace('[user:picture:url]', array('user' => $user)); doesn't work
Some node examples
<?php
// print node id
print token_replace('[node:nid]', array('node' => $node));
// print author of node picture
print token_replace('[node:author:picture]', array('node' => $node));
// node created in time since creation
print token_replace('[node:created:since]', array('node' => $node));
?>
Can someone please elaborate
Can someone please elaborate on what the $data array does. I know I need it to display my custom field using token_replace, but have no idea why. if I set $data to array('node' => $node) within my module I am returned with "undefined variable node".
what about %-tokens?
Hello!
How can I integrate tokes in my PHP code, when they have the format %token[val]?
I'm trying to write a php code for the "Confirmation message" when submitting forms using the WebForm module. There are tokens available, but with the %somthing format.
How can I integrate these tokens in my code.
Something like (this is completely invented by me!):
<?php
$token = token_replace('%get[paramter]');
print $token;
?>
where %get[paramter] ist a token available through the webform module and parameter is given by the URL "http://www.....?parameter=some_value"
I hope I made my point somehow and someone can help me. Thanks
Cheers
Pat