note 95325 added to function.ucwords

From: Date: Sun, 27 Dec 2009 15:37:41 +0000
Subject: note 95325 added to function.ucwords
Groups: php.notes 
Request: Send a blank email to php-notes+get-164352@lists.php.net to get a copy of this message
ucwords() only uses whitespace as a delimiter. I found that I needed the ability to use other delimiters for denoting words. Example: Transforming "content-type" into "Content-Type". <?php /** * Capitalize all words * @param string Data to capitalize * @param string Word delimiters * @return string Capitalized words */ function capitalizeWords($words, $charList = null) { // Use ucwords if no delimiters are given if (!isset($charList)) { return ucwords($words); } // Go through all characters $capitalizeNext = true; for ($i = 0, $max = strlen($words); $i < $max; $i++) { if (strpos($charList, $words[$i]) !== false) { $capitalizeNext = true; } else if ($capitalizeNext) { $capitalizeNext = false; $words[$i] = strtoupper($words[$i]); } } return $words; } ?> Testing: <?php echo capitalizeWords('this is a test'); // This Is A Test echo capitalizeWords('this-is-a-test'); // This-is-a-test echo capitalizeWords('this is a test', ' '); // This Is A Test echo capitalizeWords('this-is-a-test', '-'); // This-Is-A-Test echo capitalizeWords('this-is a-test', ' '); // This-is A-test echo capitalizeWords('this-is a-test', '-'); // This-Is a-Test echo capitalizeWords('this-is a-test', ' -'); // This-Is A-Test ?> ---- Server IP: 69.147.83.197 Probable Submitter: 68.50.190.12 ---- Manual Page -- http://www.php.net/manual/en/function.ucwords.php Edit -- https://master.php.net/note/edit/95325 Del: integrated -- https://master.php.net/note/delete/95325/integrated Del: useless -- https://master.php.net/note/delete/95325/useless Del: bad code -- https://master.php.net/note/delete/95325/bad+code Del: spam -- https://master.php.net/note/delete/95325/spam Del: non-english -- https://master.php.net/note/delete/95325/non-english Del: in docs -- https://master.php.net/note/delete/95325/in+docs Del: other reasons-- https://master.php.net/note/delete/95325 Reject -- https://master.php.net/note/reject/95325 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164352) next »