Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modifier.truncate.php 1.3 KiB

18 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty truncate modifier plugin
  9. *
  10. * Type: modifier<br>
  11. * Name: truncate<br>
  12. * Purpose: Truncate a string to a certain length if necessary,
  13. * optionally splitting in the middle of a word, and
  14. * appending the $etc string or inserting $etc into the middle.
  15. * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
  16. * truncate (Smarty online manual)
  17. * @author Monte Ohrt <monte at ohrt dot com>
  18. * @param string
  19. * @param integer
  20. * @param string
  21. * @param boolean
  22. * @param boolean
  23. * @return string
  24. */
  25. function smarty_modifier_truncate($string, $length = 80, $etc = '...',
  26. $break_words = false, $middle = false)
  27. {
  28. if ($length == 0)
  29. return '';
  30. if (strlen($string) > $length) {
  31. $length -= strlen($etc);
  32. if (!$break_words && !$middle) {
  33. $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
  34. }
  35. if(!$middle) {
  36. return substr($string, 0, $length).$etc;
  37. } else {
  38. return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
  39. }
  40. } else {
  41. return $string;
  42. }
  43. }
  44. /* vim: set expandtab: */
  45. ?>