From 6741f65f4a90dc0c3e0fbf00df6d9157518a2135 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Thu, 6 Jan 2011 20:04:30 +0100 Subject: [PATCH] Add ucwords(). --- framework/Util/lib/Horde/String.php | 22 ++++++++++++++++++++++ framework/Util/test/Horde/Util/StringTest.php | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) 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')) { -- 2.11.0