Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Smarty plugin | |
38 | * @package Smarty | |
39 | * @subpackage plugins | |
40 | */ | |
41 | ||
42 | /** | |
43 | * Smarty mb_truncate modifier plugin | |
44 | * | |
45 | * Type: modifier<br> | |
46 | * Name: mb_truncate<br> | |
47 | * Purpose: Truncate a string to a certain length if necessary, | |
48 | * optionally splitting in the middle of a word, and | |
49 | * appending the $etc string. Multibyte version. | |
50 | * @link http://smarty.php.net/manual/en/language.modifier.truncate.php | |
51 | * truncate (Smarty online manual) | |
52 | * | |
53 | * @param string | |
54 | * @param integer | |
55 | * @param string | |
56 | * @param boolean | |
57 | * | |
58 | * @return string | |
59 | */ | |
60 | function smarty_modifier_mb_truncate($string, $length = 80, $etc = '...', | |
61 | $break_words = FALSE | |
62 | ) { | |
63 | if (function_exists('mb_internal_encoding') and function_exists('mb_strlen') and function_exists('mb_substr')) { | |
64 | mb_internal_encoding('UTF-8'); | |
65 | $strlen = 'mb_strlen'; | |
66 | $substr = 'mb_substr'; | |
67 | } | |
68 | else { | |
69 | $strlen = 'strlen'; | |
70 | $substr = 'substr'; | |
71 | } | |
72 | ||
73 | if ($length == 0) { | |
74 | ||
75 | return ''; | |
76 | ||
77 | } | |
78 | ||
79 | if ($strlen($string) > $length) { | |
80 | $length -= $strlen($etc); | |
81 | if (!$break_words) { | |
82 | $string = preg_replace('/\s+?(\S+)?$/', '', $substr($string, 0, $length + 1)); | |
83 | } | |
84 | ||
85 | return $substr($string, 0, $length) . $etc; | |
86 | } | |
87 | else return $string; | |
88 | } | |
89 | ||
90 | /* vim: set expandtab: */ | |
91 |