Unified StringUtil::truncate() and StringUtil::truncateHTML()
authorMarcel Werk <burntime@woltlab.com>
Thu, 28 Feb 2013 20:32:26 +0000 (21:32 +0100)
committerMarcel Werk <burntime@woltlab.com>
Thu, 28 Feb 2013 20:32:26 +0000 (21:32 +0100)
Closes #1040

wcfsetup/install/files/lib/util/StringUtil.class.php

index e4e909a5b6a008992005a2aca23d08b2dd42d57e..90f7b4a546cd186567b9d155ce5f7f8db13fb1f3 100644 (file)
@@ -524,11 +524,11 @@ final class StringUtil {
        /**
         * Truncates the given string to a certain number of characters.
         * 
-        * @param       string          $string
-        * @param       integer         $length
+        * @param       string          $string         string which shall be truncated
+        * @param       integer         $length         string length after truncating
         * @param       string          $etc            string to append when $string is truncated
         * @param       boolean         $breakWords     should words be broken in the middle
-        * @return      string
+        * @return      string                          truncated string
         */
        public static function truncate($string, $length = 80, $etc = self::HELLIP, $breakWords = false) {
                if ($length == 0) {
@@ -554,11 +554,11 @@ final class StringUtil {
         *
         * @param       string          $string                 string which shall be truncated
         * @param       integer         $length                 string length after truncating
-        * @param       boolean         $wordWrap               if true words will not be split and the return string might be shorter than $length
         * @param       string          $etc                    ending string which will be appended after truncating
+        * @param       boolean         $breakWords             if false words will not be split and the return string might be shorter than $length
         * @return      string                                  truncated string
         */
-       public static function truncateHTML($string, $length = 500, $wordWrap = true, $etc = self::HELLIP) {
+       public static function truncateHTML($string, $length = 500, $etc = self::HELLIP, $breakWords = false) {
                if (self::length(self::stripHTML($string)) <= $length) {
                        return $string;
                }
@@ -595,7 +595,7 @@ final class StringUtil {
                        $decodedContent = self::decodeHTML($tag[3]);
                        $contentLength = self::length($decodedContent);
                        if ($contentLength + $totalLength > $length) {
-                               if ($wordWrap) {
+                               if (!$breakWords) {
                                        if (preg_match('/^(.{1,'.($length - $totalLength).'}) /s', $decodedContent, $match)) {
                                                $truncatedString .= self::encodeHTML($match[1]);
                                        }