Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Smarty plugin | |
20 | * @package Smarty | |
21 | * @subpackage plugins | |
22 | */ | |
23 | ||
24 | /** | |
25 | * Smarty mb_truncate modifier plugin | |
26 | * | |
27 | * Type: modifier<br> | |
28 | * Name: mb_truncate<br> | |
29 | * Purpose: Truncate a string to a certain length if necessary, | |
30 | * optionally splitting in the middle of a word, and | |
31 | * appending the $etc string. Multibyte version. | |
32 | * @link http://smarty.php.net/manual/en/language.modifier.truncate.php | |
33 | * truncate (Smarty online manual) | |
34 | * | |
79d7553f | 35 | * @param string $string |
36 | * @param int $length | |
37 | * @param string $etc | |
38 | * @param bool $break_words | |
6a488035 TO |
39 | * |
40 | * @return string | |
41 | */ | |
42 | function smarty_modifier_mb_truncate($string, $length = 80, $etc = '...', | |
353ffa53 | 43 | $break_words = FALSE |
6a488035 TO |
44 | ) { |
45 | if (function_exists('mb_internal_encoding') and function_exists('mb_strlen') and function_exists('mb_substr')) { | |
46 | mb_internal_encoding('UTF-8'); | |
47 | $strlen = 'mb_strlen'; | |
48 | $substr = 'mb_substr'; | |
49 | } | |
50 | else { | |
51 | $strlen = 'strlen'; | |
52 | $substr = 'substr'; | |
53 | } | |
54 | ||
55 | if ($length == 0) { | |
56 | ||
57 | return ''; | |
58 | ||
59 | } | |
60 | ||
61 | if ($strlen($string) > $length) { | |
62 | $length -= $strlen($etc); | |
63 | if (!$break_words) { | |
64 | $string = preg_replace('/\s+?(\S+)?$/', '', $substr($string, 0, $length + 1)); | |
65 | } | |
66 | ||
67 | return $substr($string, 0, $length) . $etc; | |
68 | } | |
4f99ca55 TO |
69 | else { |
70 | return $string; | |
2aa397bc | 71 | } |
6a488035 TO |
72 | } |
73 | ||
74 | /* vim: set expandtab: */ |