modifier.count_words.php 703 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty count_words modifier plugin
  9. *
  10. * Type: modifier<br>
  11. * Name: count_words<br>
  12. * Purpose: count the number of words in a text
  13. * @link http://smarty.php.net/manual/en/language.modifier.count.words.php
  14. * count_words (Smarty online manual)
  15. * @param string
  16. * @return integer
  17. */
  18. function smarty_modifier_count_words($string)
  19. {
  20. // split text by ' ',\r,\n,\f,\t
  21. $split_array = preg_split('/\s+/',$string);
  22. // count matches that contain alphanumerics
  23. $word_count = preg_grep('/[a-zA-Z0-9\\x80-\\xff]/', $split_array);
  24. return count($word_count);
  25. }
  26. /* vim: set expandtab: */
  27. ?>