Adding StringUtil::truncate()
authorTim Düsterhus <duesterhus@woltlab.com>
Sat, 25 Aug 2012 15:38:24 +0000 (17:38 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Sat, 25 Aug 2012 15:42:13 +0000 (17:42 +0200)
wcfsetup/install/files/lib/system/template/plugin/TruncateModifierTemplatePlugin.class.php
wcfsetup/install/files/lib/util/StringUtil.class.php

index ecbb52f5b135b2c8446a228c2cb068ac695016ba..2dfbdd82e214495b94fa5158e58e03d81dacf711 100644 (file)
@@ -7,7 +7,7 @@ use wcf\util\StringUtil;
  * The 'truncate' modifier truncates a string.
  * 
  * Usage:
- * {$foo|truncate:35:'...'}
+ * {$foo|truncate:35:' and more'}
  *
  * @author     Marcel Werk
  * @copyright  2001-2011 WoltLab GmbH
@@ -23,7 +23,7 @@ class TruncateModifierTemplatePlugin implements IModifierTemplatePlugin {
        public function execute($tagArgs, TemplateEngine $tplObj) {
                // default values
                $length = 80;
-               $etc = '...';
+               $etc = '';
                $breakWords = false;
                
                // get values
@@ -32,22 +32,6 @@ class TruncateModifierTemplatePlugin implements IModifierTemplatePlugin {
                if (isset($tagArgs[2])) $etc = $tagArgs[2];
                if (isset($tagArgs[3])) $breakWords = $tagArgs[3];
                
-               // execute plugin
-               if ($length == 0) {
-                       return '';
-               }
-
-               if (StringUtil::length($string) > $length) {
-                       $length -= StringUtil::length($etc);
-                       
-                       if (!$breakWords) {
-                               $string = preg_replace('/\s+?(\S+)?$/', '', StringUtil::substring($string, 0, $length + 1));
-                       }
-  
-                       return StringUtil::substring($string, 0, $length).$etc;
-               }
-               else {
-                       return $string;
-               }
+               return StringUtil::truncate($string, $length, $etc, $breakWords);
        }
 }
index 0278114ce149f745e3b592aeeea61621e6d1d929..c9438297a317ecdcd8f45e7a1c6cbedda8ec9d43 100644 (file)
@@ -542,6 +542,34 @@ final class StringUtil {
                return true;
        }
        
+       /**
+        * Truncates the given string to a certain number of characters.
+        * 
+        * @param       string          $string
+        * @param       integer         $length
+        * @param       string          $etc            string to append when $string is truncated
+        * @param       boolean         $breakWords     should words be broken in the middle
+        * @return      string                          truncated string
+        */
+       public static function truncate($string, $length = 80, $etc = '…', $breakWords = false) {
+               if ($length == 0) {
+                       return '';
+               }
+               
+               if (self::length($string) > $length) {
+                       $length -= self::length($etc);
+                       
+                       if (!$breakWords) {
+                               $string = preg_replace('/\\s+?(\\S+)?$/', '', self::substring($string, 0, $length + 1));
+                       }
+                       
+                       return self::substring($string, 0, $length).$etc;
+               }
+               else {
+                       return $string;
+               }
+       }
+       
        /**
         * Splits given string into smaller chunks.
         *