You are here

function _locale_import_tokenize_formula in Drupal 7

Same name and namespace in other branches
  1. 4 includes/locale.inc \_locale_import_tokenize_formula()
  2. 5 includes/locale.inc \_locale_import_tokenize_formula()
  3. 6 includes/locale.inc \_locale_import_tokenize_formula()

Backward compatible implementation of token_get_all() for formula parsing

Parameters

$string: A string containing the arithmetic formula.

Return value

The PHP version of the formula.

Related topics

1 call to _locale_import_tokenize_formula()
_locale_import_parse_arithmetic in includes/locale.inc
Parses and sanitizes an arithmetic formula into a PHP expression

File

includes/locale.inc, line 1386
Administration functions for locale.module.

Code

function _locale_import_tokenize_formula($formula) {
  $formula = str_replace(" ", "", $formula);
  $tokens = array();
  for ($i = 0; $i < strlen($formula); $i++) {
    if (is_numeric($formula[$i])) {
      $num = $formula[$i];
      $j = $i + 1;
      while ($j < strlen($formula) && is_numeric($formula[$j])) {
        $num .= $formula[$j];
        $j++;
      }
      $i = $j - 1;
      $tokens[] = $num;
    }
    elseif ($pos = strpos(" =<>!&|", $formula[$i])) {

      // We won't have a space
      $next = $formula[$i + 1];
      switch ($pos) {
        case 1:
        case 2:
        case 3:
        case 4:
          if ($next == '=') {
            $tokens[] = $formula[$i] . '=';
            $i++;
          }
          else {
            $tokens[] = $formula[$i];
          }
          break;
        case 5:
          if ($next == '&') {
            $tokens[] = '&&';
            $i++;
          }
          else {
            $tokens[] = $formula[$i];
          }
          break;
        case 6:
          if ($next == '|') {
            $tokens[] = '||';
            $i++;
          }
          else {
            $tokens[] = $formula[$i];
          }
          break;
      }
    }
    else {
      $tokens[] = $formula[$i];
    }
  }
  return $tokens;
}