6 * @subpackage PluginsShared
9 if (!function_exists('smarty_mb_wordwrap')) {
12 * Wrap a string to a given number of characters
14 * @link http://php.net/manual/en/function.wordwrap.php for similarity
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
21 * @return string wrapped string
24 function smarty_mb_wordwrap($str, $width = 75, $break = "\n", $cut = false)
26 // break words into tokens using white space as a delimiter
27 $tokens = preg_split('!(\s)!S' . Smarty::$_UTF8_MODIFIER, $str, - 1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
32 foreach ($tokens as $_token) {
33 $token_length = mb_strlen($_token, Smarty::$_CHARSET);
34 $_tokens = array($_token);
35 if ($token_length > $width) {
37 $t = mb_substr($t, 0, - 1, Smarty::$_CHARSET);
42 $_tokens = preg_split('!(.{' . $width . '})!S' . Smarty::$_UTF8_MODIFIER, $_token, - 1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
43 // broken words go on a new line
48 foreach ($_tokens as $token) {
49 $_space = !!preg_match('!^\s$!S' . Smarty::$_UTF8_MODIFIER, $token);
50 $token_length = mb_strlen($token, Smarty::$_CHARSET);
51 $length += $token_length;
53 if ($length > $width) {
54 // remove space before inserted break
55 if ($_previous && $token_length < $width) {
56 $t = mb_substr($t, 0, - 1, Smarty::$_CHARSET);
59 // add the break before the token
61 $length = $token_length;
63 // skip space after inserting a break
68 } elseif ($token == "\n") {
69 // hard break must reset counters
73 // remember if we had a space or not