Automatically exported from code.google.com/p/planningalerts
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

51 lignes
1.3 KiB

  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. ?>