| 7 token.inc | token_scan($text) |
| 8 token.inc | token_scan($text) |
Builds a list of all token-like patterns that appear in the text.
Parameters
$text: The text to be scanned for possible tokens.
Return value
An associative array of discovered tokens, grouped by type.
1 call to token_scan()
File
- includes/
token.inc, line 114 - Drupal placeholder/token replacement system.
Code
function token_scan($text) {
// Matches tokens with the following pattern: [$type:$name]
// $type and $name may not contain [ ] or whitespace characters.
// $type may not contain : characters, but $name may.
preg_match_all('/
\[ # [ - pattern start
([^\s\[\]:]*) # match $type not containing whitespace : [ or ]
: # : - separator
([^\s\[\]]*) # match $name not containing whitespace [ or ]
\] # ] - pattern end
/x', $text, $matches);
$types = $matches[1];
$tokens = $matches[2];
// Iterate through the matches, building an associative array containing
// $tokens grouped by $types, pointing to the version of the token found in
// the source text. For example, $results['node']['title'] = '[node:title]';
$results = array();
for ($i = 0; $i < count($tokens); $i++) {
$results[$types[$i]][$tokens[$i]] = $matches[0][$i];
}
return $results;
}
Login or register to post comments
Comments
dynamic tokens can't have spaces?
In the process of trying to write my own dynamic tokens I have run across the problem where I can't use spaces in tokens. I was trying to mimic the [date:custom:?] token and noticed it too had the same problems.
If I want to display a date using this token [date:custom:Y m] it wouldn't work. If I don't have a space [date:custom:Y-m] it would work fine.
I noticed that the regexp in token_scan() discard the token if it contains a space. I changed my preg_match_all line to read:
preg_match_all('/\[([^\]:]*):([^\]]*)\]/', $text, $matches);Core bug report
See core bug report http://drupal.org/node/1035292 and please help review/fix that issue.