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 or inserting $etc into the middle. |
17 | * @link http://smarty.php.net/manual/en/language.modifier.truncate.php |
18 | * truncate (Smarty online manual) |
9b7c11be |
19 | * @author Monte Ohrt <monte at ohrt dot com> |
51ff3226 |
20 | * @param string |
21 | * @param integer |
22 | * @param string |
23 | * @param boolean |
24 | * @param boolean |
25 | * @return string |
26 | */ |
27 | function smarty_modifier_truncate($string, $length = 80, $etc = '...', |
28 | $break_words = false, $middle = false) |
29 | { |
30 | if ($length == 0) |
31 | return ''; |
32 | |
33 | if (strlen($string) > $length) { |
9b7c11be |
34 | $length -= min($length, strlen($etc)); |
51ff3226 |
35 | if (!$break_words && !$middle) { |
36 | $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1)); |
37 | } |
38 | if(!$middle) { |
9b7c11be |
39 | return substr($string, 0, $length) . $etc; |
51ff3226 |
40 | } else { |
41 | return substr($string, 0, $length/2) . $etc . substr($string, -$length/2); |
42 | } |
43 | } else { |
44 | return $string; |
45 | } |
46 | } |
47 | |
48 | /* vim: set expandtab: */ |
49 | |
50 | ?> |