/framework/vendor/smarty3/lib/libs/plugins/modifier.truncate.php

http://zoop.googlecode.com/ · PHP · 64 lines · 37 code · 3 blank · 24 comment · 16 complexity · 490041fe1c6a0f9ff1dfd9a792d8f09f MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifier
  7. */
  8. /**
  9. * Smarty truncate modifier plugin
  10. *
  11. * Type: modifier<br>
  12. * Name: truncate<br>
  13. * Purpose: Truncate a string to a certain length if necessary,
  14. * optionally splitting in the middle of a word, and
  15. * appending the $etc string or inserting $etc into the middle.
  16. *
  17. * @link http://smarty.php.net/manual/en/language.modifier.truncate.php truncate (Smarty online manual)
  18. * @author Monte Ohrt <monte at ohrt dot com>
  19. * @param string $string input string
  20. * @param integer $length lenght of truncated text
  21. * @param string $etc end string
  22. * @param boolean $break_words truncate at word boundary
  23. * @param boolean $middle truncate in the middle of text
  24. * @return string truncated string
  25. */
  26. function smarty_modifier_truncate($string, $length = 80, $etc = '...',
  27. $break_words = false, $middle = false)
  28. {
  29. if ($length == 0)
  30. return '';
  31. if (is_callable('mb_strlen')) {
  32. if (mb_strlen($string) > $length) {
  33. $length -= min($length, mb_strlen($etc));
  34. if (!$break_words && !$middle) {
  35. $string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1));
  36. }
  37. if (!$middle) {
  38. return mb_substr($string, 0, $length) . $etc;
  39. } else {
  40. return mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, - $length / 2);
  41. }
  42. } else {
  43. return $string;
  44. }
  45. } else {
  46. if (strlen($string) > $length) {
  47. $length -= min($length, strlen($etc));
  48. if (!$break_words && !$middle) {
  49. $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
  50. }
  51. if (!$middle) {
  52. return substr($string, 0, $length) . $etc;
  53. } else {
  54. return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);
  55. }
  56. } else {
  57. return $string;
  58. }
  59. }
  60. }
  61. ?>