summaryrefslogtreecommitdiff
path: root/lib/smarty/plugins/modifier.mb_wordwrap.php
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2018-10-18 02:39:34 +0200
committeremkael <emkael@tlen.pl>2018-10-18 02:39:42 +0200
commitab5d8d4e07bb3c8230d0285ef8902ef1979fce51 (patch)
tree0b955e585cb2fdbc7207392a5f2c97d610b6a5bc /lib/smarty/plugins/modifier.mb_wordwrap.php
parentc055ce2ab60c6582bad3e5babcb1d00384fde78a (diff)
Updating Smarty
Diffstat (limited to 'lib/smarty/plugins/modifier.mb_wordwrap.php')
-rw-r--r--lib/smarty/plugins/modifier.mb_wordwrap.php71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/smarty/plugins/modifier.mb_wordwrap.php b/lib/smarty/plugins/modifier.mb_wordwrap.php
new file mode 100644
index 0000000..1cd625b
--- /dev/null
+++ b/lib/smarty/plugins/modifier.mb_wordwrap.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * Smarty plugin
+ *
+ * @package Smarty
+ * @subpackage PluginsModifier
+ */
+/**
+ * Smarty wordwrap modifier plugin
+ * Type: modifier
+ * Name: mb_wordwrap
+ * Purpose: 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
+ * @author Rodney Rehm
+ */
+function smarty_modifier_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);
+ $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) {
+ if ($cut) {
+ $_tokens = preg_split(
+ '!(.{' . $width . '})!S' . Smarty::$_UTF8_MODIFIER,
+ $_token,
+ -1,
+ PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE
+ );
+ }
+ }
+ foreach ($_tokens as $token) {
+ $_space = !!preg_match('!^\s$!S' . Smarty::$_UTF8_MODIFIER, $token);
+ $token_length = mb_strlen($token, Smarty::$_CHARSET);
+ $length += $token_length;
+ if ($length > $width) {
+ // remove space before inserted break
+ if ($_previous) {
+ $t = mb_substr($t, 0, -1, Smarty::$_CHARSET);
+ }
+ if (!$_space) {
+ // add the break before the token
+ if (!empty($t)) {
+ $t .= $break;
+ }
+ $length = $token_length;
+ }
+ } elseif ($token === "\n") {
+ // hard break must reset counters
+ $length = 0;
+ }
+ $_previous = $_space;
+ // add the token
+ $t .= $token;
+ }
+ }
+ return $t;
+}