From: Jan Schneider Date: Thu, 6 Jan 2011 19:04:30 +0000 (+0100) Subject: Add ucwords(). X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=6741f65f4a90dc0c3e0fbf00df6d9157518a2135;p=horde.git Add ucwords(). --- diff --git a/framework/Util/lib/Horde/String.php b/framework/Util/lib/Horde/String.php index 2fc301b95..f282e8a68 100644 --- a/framework/Util/lib/Horde/String.php +++ b/framework/Util/lib/Horde/String.php @@ -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. diff --git a/framework/Util/test/Horde/Util/StringTest.php b/framework/Util/test/Horde/Util/StringTest.php index 7b38969bb..d035538d6 100644 --- a/framework/Util/test/Horde/Util/StringTest.php +++ b/framework/Util/test/Horde/Util/StringTest.php @@ -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')) {