Add ucwords().
authorJan Schneider <jan@horde.org>
Thu, 6 Jan 2011 19:04:30 +0000 (20:04 +0100)
committerJan Schneider <jan@horde.org>
Tue, 11 Jan 2011 14:21:31 +0000 (15:21 +0100)
framework/Util/lib/Horde/String.php
framework/Util/test/Horde/Util/StringTest.php

index 2fc301b..f282e8a 100644 (file)
@@ -269,6 +269,28 @@ class Horde_String
     }
 
     /**
+     * Returns a string with the first letter of each word capitalized if it is
+     * alphabetic.
+     *
+     * Sentences are splitted into words at whitestrings.
+     *
+     * @param string $string   The string to be capitalized.
+     * @param boolean $locale  If true the string will be converted based on a
+     *                         given charset, locale independent else.
+     * @param string $charset  The charset to use, defaults to current charset.
+     *
+     * @return string  The capitalized string.
+     */
+    static public function ucwords($string, $locale = false, $charset = null)
+    {
+        $words = preg_split('/(\s+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
+        for ($i = 0, $c = count($words); $i < $c; $i += 2) {
+            $words[$i] = Horde_String::ucfirst($words[$i], $locale, $charset);
+        }
+        return implode('', $words);
+    }
+
+    /**
      * Returns part of a string.
      *
      * @param string $string   The string to be converted.
index 7b38969..d035538 100644 (file)
@@ -93,6 +93,24 @@ class Horde_Util_StringTest extends PHPUnit_Framework_TestCase
         );
     }
 
+    public function testUcwords()
+    {
+        $this->assertEquals(
+            'Integer  Inside',
+            Horde_String::ucwords('integer  inside', true, 'us-ascii')
+        );
+        $this->assertEquals(
+            'Integer  Inside',
+            Horde_String::ucwords('integer  inside', true, 'Big5')
+        );
+        $this->assertEquals(
+            'İnteger  İnside',
+            Horde_String::convertCharset(
+                Horde_String::ucwords('integer  inside', true, 'iso-8859-9'),
+                'iso-8859-9', 'utf-8')
+        );
+    }
+
     public function testUcfirstTurkish()
     {
         if (!setlocale(LC_ALL, 'tr_TR')) {