1 |
cvsjoko |
1.1 |
<?php |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Smarty plugin |
5 |
|
|
* ------------------------------------------------------------- |
6 |
|
|
* Type: modifier |
7 |
|
|
* Name: truncate |
8 |
|
|
* Purpose: Truncate a string to a certain length if necessary, |
9 |
|
|
* optionally splitting in the middle of a word, and |
10 |
|
|
* appending the $etc string. |
11 |
|
|
* ------------------------------------------------------------- |
12 |
|
|
*/ |
13 |
|
|
function smarty_modifier_truncate($string, $length = 80, $etc = '...', |
14 |
|
|
$break_words = false) |
15 |
|
|
{ |
16 |
|
|
if ($length == 0) |
17 |
|
|
return ''; |
18 |
|
|
|
19 |
|
|
if (strlen($string) > $length) { |
20 |
|
|
$length -= strlen($etc); |
21 |
|
|
$fragment = substr($string, 0, $length+1); |
22 |
|
|
if ($break_words) |
23 |
|
|
$fragment = substr($fragment, 0, -1); |
24 |
|
|
else |
25 |
|
|
$fragment = preg_replace('/\s+(\S+)?$/', '', $fragment); |
26 |
|
|
return $fragment.$etc; |
27 |
|
|
} else |
28 |
|
|
return $string; |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
/* vim: set expandtab: */ |
32 |
|
|
|
33 |
|
|
?> |