You are here

function typogrify_smart_numbers in Typogrify 7

Adding space in numbers for easier reading aka digit grouping.

Parameters

string $text: to work on.

int $attr: kind of space to use.

array $ctx: not used.

1 call to typogrify_smart_numbers()
_typogrify_process in ./typogrify.module
Processing function to apply the Typogrify filters.

File

./smartypants.php, line 566
SmartyPants - Smart punctuation for web sites

Code

function typogrify_smart_numbers($text, $attr = 0, $ctx = NULL) {
  $tokens;
  $tokens = _TokenizeHTML($text);
  if ($attr == 0) {
    return $text;
  }
  elseif ($attr == 1) {
    $method = '_typogrify_number_narrownbsp';
  }
  elseif ($attr == 2) {
    $method = '_typogrify_number_thinsp';
  }
  elseif ($attr == 3) {
    $method = '_typogrify_number_span';
  }
  elseif ($attr == 4) {
    $method = '_typogrify_number_just_span';
  }
  $result = '';

  // Keep track of when we're inside <pre> or <code> tags.
  $in_pre = 0;
  $span_stop = 0;
  foreach ($tokens as $cur_token) {
    if ($cur_token[0] == "tag") {

      // Don't mess with quotes inside tags.
      $result .= $cur_token[1];
      if (preg_match(SMARTYPANTS_TAGS_TO_SKIP, $cur_token[1], $matches)) {
        $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
      }
      elseif (preg_match('/<span .*class="[^"]*\\b(number|phone|ignore)\\b[^"]*"/', $cur_token[1], $matches)) {
        $span_stop = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
      }
      elseif ($cur_token[1] == '</span>') {
        $span_stop = 0;
      }
    }
    else {
      $t = $cur_token[1];
      if (!$in_pre && !$span_stop) {
        $number_finder = '@(?:(&#\\d{2,4};|&(#x[0-9a-fA-F]{2,4}|frac\\d\\d);)|' . '(\\d{4}-\\d\\d-\\d\\d)|(\\d\\d\\.\\d\\d\\.\\d{4})|' . '(0[ \\d-/]+)|([+-]?\\d+)([.,]\\d+|))@';
        $t = preg_replace_callback($number_finder, $method, $t);
      }
      $result .= $t;
    }
  }
  return $result;
}