Add Horde_String::truncate()
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 9 Jul 2009 22:54:25 +0000 (16:54 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Sun, 12 Jul 2009 20:00:26 +0000 (14:00 -0600)
framework/Util/lib/Horde/String.php

index 61e9606..bc60e69 100644 (file)
@@ -589,6 +589,21 @@ class Horde_String
     }
 
     /**
+     * Return a truncated string, suitable for notifications.
+     *
+     * @param string $text     The original string.
+     * @param integer $length  The maximum length.
+     *
+     * @return string  The truncated string, if longer than $length.
+     */
+    static public function truncate($text, $length = 100)
+    {
+        return (self::length($text) > $length)
+            ? self::substr($text, 0, 100) . ' ...'
+            : $text;
+    }
+
+    /**
      * Returns true if the every character in the parameter is an alphabetic
      * character.
      *