init
[garradin.git] / include / libs / template_lite / plugins / modifier.truncate.php
1 <?php
2 /**
3 * template_lite truncate modifier plugin
4 *
5 * Type: modifier
6 * Name: truncate
7 * Purpose: Truncate a string to a certain length if necessary,
8 * optionally splitting in the middle of a word, and
9 * appending the $etc string.
10 * Credit: Taken from the original Smarty
11 * http://smarty.php.net
12 */
13 function tpl_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false)
14 {
15 if ($length == 0)
16 {
17 return '';
18 }
19
20 if (strlen($string) > $length)
21 {
22 $length -= strlen($etc);
23 if (!$break_words)
24 {
25 $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
26 }
27 return substr($string, 0, $length).$etc;
28 }
29 else
30 {
31 return $string;
32 }
33 }
34 ?>