From 5d3505bfeca6d76cf5b5bee96cf47a017efda43c Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 17 Apr 2013 02:40:14 +0200 Subject: [PATCH] Added simple wordwrap function --- .../TruncateModifierTemplatePlugin.class.php | 2 +- .../WordwrapModifierTemplatePlugin.class.php | 35 +++++++++++++++++++ .../files/lib/util/StringUtil.class.php | 32 +++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 wcfsetup/install/files/lib/system/template/plugin/WordwrapModifierTemplatePlugin.class.php diff --git a/wcfsetup/install/files/lib/system/template/plugin/TruncateModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/TruncateModifierTemplatePlugin.class.php index 8206dad6c9..a13109fad3 100644 --- a/wcfsetup/install/files/lib/system/template/plugin/TruncateModifierTemplatePlugin.class.php +++ b/wcfsetup/install/files/lib/system/template/plugin/TruncateModifierTemplatePlugin.class.php @@ -23,7 +23,7 @@ class TruncateModifierTemplatePlugin implements IModifierTemplatePlugin { public function execute($tagArgs, TemplateEngine $tplObj) { // default values $length = 80; - $etc = '…'; + $etc = StringUtil::HELLIP; $breakWords = false; // get values diff --git a/wcfsetup/install/files/lib/system/template/plugin/WordwrapModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/WordwrapModifierTemplatePlugin.class.php new file mode 100644 index 0000000000..9c446937c4 --- /dev/null +++ b/wcfsetup/install/files/lib/system/template/plugin/WordwrapModifierTemplatePlugin.class.php @@ -0,0 +1,35 @@ + + * @package com.woltlab.wcf + * @subpackage system.template.plugin + * @category Community Framework + */ +class WordwrapModifierTemplatePlugin implements IModifierTemplatePlugin { + /** + * @see wcf\system\template\IModifierTemplatePlugin::execute() + */ + public function execute($tagArgs, TemplateEngine $tplObj) { + // default values + $width = 50; + $break = ' '; + + // get values + $string = $tagArgs[0]; + if (isset($tagArgs[1])) $width = intval($tagArgs[1]); + if (isset($tagArgs[2])) $break = $tagArgs[2]; + + return StringUtil::wordwrap($string, $width, $break); + } +} diff --git a/wcfsetup/install/files/lib/util/StringUtil.class.php b/wcfsetup/install/files/lib/util/StringUtil.class.php index 456bc41e24..4750cda945 100644 --- a/wcfsetup/install/files/lib/util/StringUtil.class.php +++ b/wcfsetup/install/files/lib/util/StringUtil.class.php @@ -695,5 +695,37 @@ final class StringUtil { return mb_ereg_replace('.{'.$length.'}', "\\0".$break, $string); } + /** + * Simple multi-byte safe wordwrap() function. + * + * @param string $string + * @param integer $width + * @param string $break + * @return string + */ + public static function wordwrap($string, $width = 50, $break = ' ') { + $result = ''; + $substrings = explode($break, $string); + + foreach ($substrings as $substring) { + $length = self::length($substring); + if ($length > $width) { + $j = ceil($length / $width); + + for ($i = 0; $i < $j; $i++) { + if (!empty($result)) $result .= $break; + if ($width * ($i + 1) > $length) $result .= self::substring($substring, $width * $i); + else $result .= self::substring($substring, $width * $i, $width); + } + } + else { + if (!empty($result)) $result .= $break; + $result .= $substring; + } + } + + return $result; + } + private function __construct() { } } -- 2.20.1