diff options
author | plegall <plg@piwigo.org> | 2015-12-30 16:21:32 +0100 |
---|---|---|
committer | plegall <plg@piwigo.org> | 2015-12-30 16:21:32 +0100 |
commit | 3ec3cbe6cec5968d29cb11af139123191f4cb4ee (patch) | |
tree | 34aecd51071d63f8df1862a1a11898d8c43fe68a /include/smarty/libs/plugins/shared.mb_wordwrap.php | |
parent | 6ba0148e646b2a193dc4111bb0a443d8c193e646 (diff) | |
parent | 1681b02ee98c2deb740d394280a2a685170bc72e (diff) |
Merge branch 'bug/385-php7'
Diffstat (limited to '')
-rw-r--r-- | include/smarty/libs/plugins/shared.mb_wordwrap.php | 60 |
1 files changed, 25 insertions, 35 deletions
diff --git a/include/smarty/libs/plugins/shared.mb_wordwrap.php b/include/smarty/libs/plugins/shared.mb_wordwrap.php index ba3498c70..31f4acf01 100644 --- a/include/smarty/libs/plugins/shared.mb_wordwrap.php +++ b/include/smarty/libs/plugins/shared.mb_wordwrap.php @@ -2,44 +2,40 @@ /** * Smarty shared plugin * - * @package Smarty + * @package Smarty * @subpackage PluginsShared */ -if(!function_exists('smarty_mb_wordwrap')) { +if (!function_exists('smarty_mb_wordwrap')) { /** * Wrap a string to a given number of characters * - * @link http://php.net/manual/en/function.wordwrap.php for similarity - * @param string $str the string to wrap - * @param int $width the width of the output - * @param string $break the character used to break the line - * @param boolean $cut ignored parameter, just for the sake of - * @return string wrapped string + * @link http://php.net/manual/en/function.wordwrap.php for similarity + * + * @param string $str the string to wrap + * @param int $width the width of the output + * @param string $break the character used to break the line + * @param boolean $cut ignored parameter, just for the sake of + * + * @return string wrapped string * @author Rodney Rehm */ - function smarty_mb_wordwrap($str, $width=75, $break="\n", $cut=false) + function smarty_mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) { // break words into tokens using white space as a delimiter - $tokens = preg_split('!(\s)!S' . Smarty::$_UTF8_MODIFIER, $str, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE); + $tokens = preg_split('!(\s)!S' . Smarty::$_UTF8_MODIFIER, $str, - 1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE); $length = 0; $t = ''; $_previous = false; + $_space = false; foreach ($tokens as $_token) { $token_length = mb_strlen($_token, Smarty::$_CHARSET); $_tokens = array($_token); if ($token_length > $width) { - // remove last space - $t = mb_substr($t, 0, -1, Smarty::$_CHARSET); - $_previous = false; - $length = 0; - - if ($cut) { - $_tokens = preg_split('!(.{' . $width . '})!S' . Smarty::$_UTF8_MODIFIER, $_token, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE); - // broken words go on a new line - $t .= $break; + if ($cut) { + $_tokens = preg_split('!(.{' . $width . '})!S' . Smarty::$_UTF8_MODIFIER, $_token, - 1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE); } } @@ -50,27 +46,23 @@ if(!function_exists('smarty_mb_wordwrap')) { if ($length > $width) { // remove space before inserted break - if ($_previous && $token_length < $width) { - $t = mb_substr($t, 0, -1, Smarty::$_CHARSET); + if ($_previous) { + $t = mb_substr($t, 0, - 1, Smarty::$_CHARSET); } - // add the break before the token - $t .= $break; - $length = $token_length; - - // skip space after inserting a break - if ($_space) { - $length = 0; - continue; + if (!$_space) { + // add the break before the token + if (!empty($t)) { + $t .= $break; + } + $length = $token_length; } - } else if ($token == "\n") { + } elseif ($token == "\n") { // hard break must reset counters $_previous = 0; $length = 0; - } else { - // remember if we had a space or not - $_previous = $_space; } + $_previous = $_space; // add the token $t .= $token; } @@ -78,6 +70,4 @@ if(!function_exists('smarty_mb_wordwrap')) { return $t; } - } -?>
\ No newline at end of file |