1 |
<?php |
<?php |
2 |
|
/** |
|
/* |
|
3 |
* Smarty plugin |
* Smarty plugin |
4 |
* ------------------------------------------------------------- |
* @package Smarty |
5 |
* Type: modifier |
* @subpackage plugins |
6 |
* Name: truncate |
*/ |
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, |
* Purpose: Truncate a string to a certain length if necessary, |
15 |
* optionally splitting in the middle of a word, and |
* optionally splitting in the middle of a word, and |
16 |
* appending the $etc string. |
* 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 = '...', |
function smarty_modifier_truncate($string, $length = 80, $etc = '...', |
26 |
$break_words = false) |
$break_words = false) |
30 |
|
|
31 |
if (strlen($string) > $length) { |
if (strlen($string) > $length) { |
32 |
$length -= strlen($etc); |
$length -= strlen($etc); |
33 |
$fragment = substr($string, 0, $length+1); |
if (!$break_words) |
34 |
if ($break_words) |
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1)); |
35 |
$fragment = substr($fragment, 0, -1); |
|
36 |
else |
return substr($string, 0, $length).$etc; |
|
$fragment = preg_replace('/\s+(\S+)?$/', '', $fragment); |
|
|
return $fragment.$etc; |
|
37 |
} else |
} else |
38 |
return $string; |
return $string; |
39 |
} |
} |