You are here

function tmgmt_word_count in Translation Management Tool 7

Calculates number of words, which a text consists of. Is placed as a separately function to be coverable by unit tests.

Parameters

string $text:

Return value

int Returns count of words of text.

See also

TMGMTWordCountUnitTestCase

3 calls to tmgmt_word_count()
TMGMTHelperTestCase::testWordCount in tests/tmgmt.helper.test
TMGMTJobItem::count in entity/tmgmt.entity.job_item.inc
Parse all data items recursively and sums up the counters for accepted, translated and pending items.
_tmgmt_data_count_7006 in ./tmgmt.install
Data parsing helper function for tmgmt_update_7006().

File

./tmgmt.module, line 1535
Main module file for the Translation Management module.

Code

function tmgmt_word_count($text) {

  // Strip tags in case it is requested to not include them in the count.
  if (variable_get('tmgmt_word_count_exclude_tags', TRUE)) {
    $text = strip_tags($text);
  }

  // Replace each punctuation mark with space.
  $text = str_replace(array(
    '`',
    '~',
    '!',
    '@',
    '"',
    '#',
    '$',
    ';',
    '%',
    '^',
    ':',
    '?',
    '&',
    '*',
    '(',
    ')',
    '-',
    '_',
    '+',
    '=',
    '{',
    '}',
    '[',
    ']',
    '\\',
    '|',
    '/',
    '\'',
    '<',
    '>',
    ',',
    '.',
  ), ' ', $text);

  // Remove duplicate spaces.
  $text = trim(preg_replace('/ {2,}/', ' ', $text));

  // Turn into an array.
  $array = $text ? explode(' ', $text) : array();

  // How many are they?
  $count = count($array);

  // That is what we need.
  return $count;
}