note 30982 added to function.ucwords
| From: | Justin at gha dot bravepages dot com | Date: | Sat, 05 Apr 2003 10:39:32 +0000 |
| Subject: | note 30982 added to function.ucwords | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46249@lists.php.net to get a copy of this message | ||
Here's my swing on things
function limited_ucfirst($text, $min_word_len = 4,
$always_cap_first = true,
$exclude =
Array("of","a","the ","and","an",
"or","nor","but","is",
"if","then","else","when","up",
"at","from","by","on",
"off","for","in","out","over",
"to","into", "with")) {
// Allows for the specification of the minimum length
// of characters each word must be in order to be capitalized
// Make sure words following punctuation are capitalized
$text = str_replace(
Array("(", "-", ".", "?",
",",":","[",";","!"),
Array("( ", "- ", ". ", "? ", ", ",":
","[ ","; ","! "), $text);
$words = explode (" ", strtolower($text));
$count = count($words);
$num = 0;
while ($num < $count) {
if (strlen($words[$num]) >= $min_word_len
&& array_search($words[$num], $exclude) === false)
$words[$num] = ucfirst($words[$num]);
$num++;
}
$text = implode(" ", $words);
$text = str_replace(
Array("( ", "- ", ". ", "? ", ", ",":
","[ ","; ","! "),
Array("(", "-", ".", "?",
",",":","[",";","!"), $text);
// Always capitalize first char if cap_first is true
if ($always_cap_first) {
if (ctype_alpha($text[0]) && ord($text[0]) <= ord("z")
&& ord($text[0]) > ord("Z"))
$text[0] = chr(ord($text[0]) - 32);
}
return $text;
}
Thanks to mgm and un-thesis for their last two posts. It was helpful in fine tuning this funcion.
--
http://www.php.net/manual/en/function.ucwords.php
http://master.php.net/manage/user-notes.php?action=edit+30982
http://master.php.net/manage/user-notes.php?action=delete+30982
http://master.php.net/manage/user-notes.php?action=reject+30982