library/smarty/libs/plugins/shared.mb_wordwrap.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty shared plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsShared
       
     7  */
       
     8 
       
     9 if (!function_exists('smarty_mb_wordwrap')) {
       
    10 
       
    11     /**
       
    12      * Wrap a string to a given number of characters
       
    13      *
       
    14      * @link   http://php.net/manual/en/function.wordwrap.php for similarity
       
    15      *
       
    16      * @param  string $str the string to wrap
       
    17      * @param  int $width the width of the output
       
    18      * @param  string $break the character used to break the line
       
    19      * @param  boolean $cut ignored parameter, just for the sake of
       
    20      *
       
    21      * @return string  wrapped string
       
    22      * @author Rodney Rehm
       
    23      */
       
    24     function smarty_mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) {
       
    25         // break words into tokens using white space as a delimiter
       
    26         $tokens = preg_split('!(\s)!S' . Smarty::$_UTF8_MODIFIER, $str, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
       
    27         $length = 0;
       
    28         $t = '';
       
    29         $_previous = false;
       
    30         $_space = false;
       
    31 
       
    32         foreach ($tokens as $_token) {
       
    33             $token_length = mb_strlen($_token, Smarty::$_CHARSET);
       
    34             $_tokens = array($_token);
       
    35             if ($token_length > $width) {
       
    36                 if ($cut) {
       
    37                     $_tokens = preg_split('!(.{' . $width . '})!S' . Smarty::$_UTF8_MODIFIER, $_token, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
       
    38                 }
       
    39             }
       
    40 
       
    41             foreach ($_tokens as $token) {
       
    42                 $_space = !!preg_match('!^\s$!S' . Smarty::$_UTF8_MODIFIER, $token);
       
    43                 $token_length = mb_strlen($token, Smarty::$_CHARSET);
       
    44                 $length += $token_length;
       
    45 
       
    46                 if ($length > $width) {
       
    47                     // remove space before inserted break
       
    48                     if ($_previous) {
       
    49                         $t = mb_substr($t, 0, -1, Smarty::$_CHARSET);
       
    50                     }
       
    51 
       
    52                     if (!$_space) {
       
    53                         // add the break before the token
       
    54                         if (!empty($t)) {
       
    55                             $t .= $break;
       
    56                         }
       
    57                         $length = $token_length;
       
    58                     }
       
    59                 } elseif ($token == "\n") {
       
    60                     // hard break must reset counters
       
    61                     $_previous = 0;
       
    62                     $length = 0;
       
    63                 }
       
    64                 $_previous = $_space;
       
    65                 // add the token
       
    66                 $t .= $token;
       
    67             }
       
    68         }
       
    69 
       
    70         return $t;
       
    71     }
       
    72 }