1: <?php
2: 3: 4: 5: 6: 7:
8:
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false)
29: {
30: if ($length == 0) {
31: return '';
32: }
33:
34: if (Smarty::$_MBSTRING) {
35: if (mb_strlen($string, Smarty::$_CHARSET) > $length) {
36: $length -= min($length, mb_strlen($etc, Smarty::$_CHARSET));
37: if (!$break_words && !$middle) {
38: $string = preg_replace('/\s+?(\S+)?$/' . Smarty::$_UTF8_MODIFIER, '', mb_substr($string, 0, $length + 1, Smarty::$_CHARSET));
39: }
40: if (!$middle) {
41: return mb_substr($string, 0, $length, Smarty::$_CHARSET) . $etc;
42: }
43:
44: return mb_substr($string, 0, $length / 2, Smarty::$_CHARSET) . $etc . mb_substr($string, - $length / 2, $length, Smarty::$_CHARSET);
45: }
46:
47: return $string;
48: }
49:
50:
51: if (isset($string[$length])) {
52: $length -= min($length, strlen($etc));
53: if (!$break_words && !$middle) {
54: $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
55: }
56: if (!$middle) {
57: return substr($string, 0, $length) . $etc;
58: }
59:
60: return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);
61: }
62:
63: return $string;
64: }
65: