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