library/smarty/libs/plugins/modifier.truncate.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsModifier
       
     7  */
       
     8 
       
     9 /**
       
    10  * Smarty truncate modifier plugin
       
    11  * Type:     modifier<br>
       
    12  * Name:     truncate<br>
       
    13  * Purpose:  Truncate a string to a certain length if necessary,
       
    14  *               optionally splitting in the middle of a word, and
       
    15  *               appending the $etc string or inserting $etc into the middle.
       
    16  *
       
    17  * @link   http://smarty.php.net/manual/en/language.modifier.truncate.php truncate (Smarty online manual)
       
    18  * @author Monte Ohrt <monte at ohrt dot com>
       
    19  *
       
    20  * @param string $string input string
       
    21  * @param integer $length length of truncated text
       
    22  * @param string $etc end string
       
    23  * @param boolean $break_words truncate at word boundary
       
    24  * @param boolean $middle truncate in the middle of text
       
    25  *
       
    26  * @return string truncated string
       
    27  */
       
    28 function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) {
       
    29     if ($length == 0) {
       
    30         return '';
       
    31     }
       
    32 
       
    33     if (Smarty::$_MBSTRING) {
       
    34         if (mb_strlen($string, Smarty::$_CHARSET) > $length) {
       
    35             $length -= min($length, mb_strlen($etc, Smarty::$_CHARSET));
       
    36             if (!$break_words && !$middle) {
       
    37                 $string = preg_replace('/\s+?(\S+)?$/' . Smarty::$_UTF8_MODIFIER, '', mb_substr($string, 0, $length + 1, Smarty::$_CHARSET));
       
    38             }
       
    39             if (!$middle) {
       
    40                 return mb_substr($string, 0, $length, Smarty::$_CHARSET) . $etc;
       
    41             }
       
    42 
       
    43             return mb_substr($string, 0, $length / 2, Smarty::$_CHARSET) . $etc . mb_substr($string, -$length / 2, $length, Smarty::$_CHARSET);
       
    44         }
       
    45 
       
    46         return $string;
       
    47     }
       
    48 
       
    49     // no MBString fallback
       
    50     if (isset($string[$length])) {
       
    51         $length -= min($length, strlen($etc));
       
    52         if (!$break_words && !$middle) {
       
    53             $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
       
    54         }
       
    55         if (!$middle) {
       
    56             return substr($string, 0, $length) . $etc;
       
    57         }
       
    58 
       
    59         return substr($string, 0, $length / 2) . $etc . substr($string, -$length / 2);
       
    60     }
       
    61 
       
    62     return $string;
       
    63 }