note 96093 added to function.ucwords
| From: | (jarrod@squarecrow.com) | Date: | Mon, 08 Feb 2010 17:34:28 +0000 |
| Subject: | note 96093 added to function.ucwords | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165757@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 192.41.40.227
Probable Submitter: 216.201.124.166
----
Manual Page -- http://www.php.net/manual/en/function.ucwords.php
Edit -- https://master.php.net/note/edit/96093
Del: integrated -- https://master.php.net/note/delete/96093/integrated
Del: useless -- https://master.php.net/note/delete/96093/useless
Del: bad code -- https://master.php.net/note/delete/96093/bad+code
Del: spam -- https://master.php.net/note/delete/96093/spam
Del: non-english -- https://master.php.net/note/delete/96093/non-english
Del: in docs -- https://master.php.net/note/delete/96093/in+docs
Del: other reasons-- https://master.php.net/note/delete/96093
Reject -- https://master.php.net/note/reject/96093
Search -- https://master.php.net/manage/user-notes.php