note 96093 deleted from function.ucwords by danbrown

From: Date: Mon, 08 Feb 2010 17:36:46 +0000
Subject: note 96093 deleted from function.ucwords by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165758@lists.php.net to get a copy of this message
Note Submitter: Jarrod Nettles (jarrod at squarecrow dot com) ---- Kovik's example works well when you only have one additional delimiter to check against but it becomes impractical to run the function multiple times for multiple delimiters. This is Kovik's function rewritten to use an array of delimiters, plus some additional options that offer more control over the process. <?php /** * Capitalize letters progressively based upon an array of delimiters. * @param string $string String to operate against. * @param array $charList Array of character delimiters. The character after each delimiter will be capitalized. * @param int $begin The string position to start on. * @param int $end The string position to end on, or -1 for the entire string. * @param bool $capfirst Capitalize the first letter by default. * @return string */ function capitalizeWords($string, array $charList = null, $begin = 0, $end = -1, $capfirst = true) { if(!isset($charList)) return ucwords($string); $i = $begin; if($end < 0): $max = strlen($string); else: $max = $end; endif; //capitalize the first letter and move on if so desired if($capfirst): $string[$i] = strtoupper($string[$i]); $i++; endif; for($i, $max; $i < $max; $i++) { foreach($charList as $char) { if(strpos($char, $string[$i]) !==false) { $string[$i+1] = strtoupper($string[$i+1]); break; } } } return $string; } $charlist = array(" ", "-", "/", ".", "*"); echo capitalizeWords('the shaw-fujikawa trans-light engine/slip-space drive. see the *brochure for details' , $charlist); ?>

« previous php.notes (#165758) next »