You are here

public static function WebformTextHelper::wordCount in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Utility/WebformTextHelper.php \Drupal\webform\Utility\WebformTextHelper::wordCount()

Counts the number of words inside a string.

This counts the number of words by counting the space between the words.

str_word_count() is locale dependent and returns varying word counts based on the current language.

This approach matches how the jQuery Text Counter Plugin counts words.

Parameters

string $string: The string.

Return value

int The number of words inside the string.

See also

str_word_count()

https://github.com/ractoon/jQuery-Text-Counter

$.textcounter.wordCount

1 call to WebformTextHelper::wordCount()
TextBase::validateCounter in src/Plugin/WebformElement/TextBase.php
Form API callback. Validate (word/character) counter.

File

src/Utility/WebformTextHelper.php, line 78

Class

WebformTextHelper
Provides helper to operate on strings.

Namespace

Drupal\webform\Utility

Code

public static function wordCount($string) {
  return count(explode(' ', preg_replace('#\\s+#', ' ', trim($string))));
}