You are here

function token_scan in Token 6

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

Build a list of all token-like patterns that appear in the text.

Parameters

$text: The text to be scanned for possible tokens.

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

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

Return value

An array of discovered tokens.

4 calls to token_scan()
TokenUnitTestCase::testTokenScan in ./token.test
Test the token_scan() function.
token_element_validate in ./token.module
Validate a form element that should have tokens in it.
token_get_invalid_tokens_by_context in ./token.module
Validate an tokens in raw text based on possible contexts.
token_replace_multiple in ./token.module
Replace all tokens in a given string with appropriate values.

File

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

Code

function token_scan($text, $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX) {
  $leadingregex = preg_quote($leading, '/');
  $trailingregex = preg_quote($trailing, '/');
  $regex = '/' . $leadingregex;
  $regex .= '([^\\s';
  if (drupal_strlen($leading) == 1) {

    // Only add the leading string as a non-match if it is a single character.
    $regex .= $leadingregex;
  }
  if (drupal_strlen($trailing) == 1) {

    // Only add the trailing string as a non-match if it is a single character.
    $regex .= $trailingregex;
  }
  $regex .= ']+)' . $trailingregex . '/x';
  preg_match_all($regex, $text, $matches);
  return $matches[1];
}