1 |
cvsjoko |
1.1 |
<?php |
2 |
joko |
1.2 |
/** |
3 |
|
|
* Smarty plugin |
4 |
|
|
* @package Smarty |
5 |
|
|
* @subpackage plugins |
6 |
|
|
*/ |
7 |
cvsjoko |
1.1 |
|
8 |
joko |
1.2 |
|
9 |
|
|
/** |
10 |
|
|
* Smarty truncate modifier plugin |
11 |
|
|
* |
12 |
|
|
* Type: modifier<br> |
13 |
|
|
* Name: truncate<br> |
14 |
cvsjoko |
1.1 |
* Purpose: Truncate a string to a certain length if necessary, |
15 |
joko |
1.2 |
* optionally splitting in the middle of a word, and |
16 |
cvsjoko |
1.1 |
* appending the $etc string. |
17 |
joko |
1.2 |
* @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 |
cvsjoko |
1.1 |
*/ |
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 |
joko |
1.2 |
if (!$break_words) |
34 |
|
|
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1)); |
35 |
|
|
|
36 |
|
|
return substr($string, 0, $length).$etc; |
37 |
cvsjoko |
1.1 |
} else |
38 |
|
|
return $string; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/* vim: set expandtab: */ |
42 |
|
|
|
43 |
|
|
?> |