note 64838 deleted from function.ucwords by bjori
| From: | bjori@php.net | Date: | Fri, 21 Apr 2006 15:35:33 +0000 |
| Subject: | note 64838 deleted from function.ucwords by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-108936@lists.php.net to get a copy of this message | ||
Note Submitter: egingell at sisna dot com
Reason: bad code
----
A smarter ucwords(). If the "word" begins with a special character, smart_ucwords() will
capitalize the second character.
<?
function smart_ucwords($str) {
$str = trim(strtolower($str));
// First get all the words
$words = explode(' ', $str);
while (list(, $word) = each($words)) {
if (preg_match("#[\/\\\\!@\\#$%^&*()_+=-><;:'\\"{}\\]\\[?\\.]#",
$word{0}, $matches)) {
$sub = substr($word, 1);
} else {
$sub = $word;
}
if (strlen($word) == 1) {
$sub = $word;
}
$new_words[] = ($matches && strlen($word) != 1 ? $matches[0] : '') .
ucfirst($sub);
}
$str = ucfirst(implode(' ', $new_words));
return $str;
}
?>