Hi everyone,
I have some code that serves to capitalize the first letter of every word in a sentence, except in certain cases (see below).
Rules (rules lower on the list override rules higher on the list in case of a conflict):
[ol][li]First letter of every word in a sentence needs to be capitalized, all others lowercase.[/li]
[li]If word is on $exclude_words list, make sure it stays all lowercase.[/li]
[li]If word is on $exclude_words list BUT is the FIRST word in a sentence, capitalize first letter and lowercase the remaining letters in that word.[/li]
[li]If word is on $exclude_words list, BUT is the LAST word in a sentence, capitalize first letter and lowercase the remaining letters in that word.[/li]
[li]If word is entered in all caps and is NOT on $capital_words AND is less than $max_length, leave it in all caps.[/li]
[li]If word is entered in all caps and is NOT on $capital_words AND is greater than $max_length, capitalize first letter only.[/li]
[li]If word is on $capital_words list, capitalize all letters in that word, regardless of position in sentence.[/li]
[li]If word contains punctuation (comma, exclamation mark, question mark, period, hyphen, forward or back slash, quotes,etc), separate words from punctuation and capitalize based on previous rules.[/li][/ol]
So far my code is properly working on all of these rules except for Rule #4. I am not sure how to code this to make it work with Rule #4 (bolded above) while still satisfying all of the other rules.
Here is my code. Would love any help that anyone can provide. Thank you.
[PHP]if (is_subclass_of($this, ‘vB_DataManager_ThreadPost’) && is_array($this->validfields[‘title’])) {
$this->validfields[‘title’][VF_CODE] = ’
global $exclude_words, $capital_words, $max_length;
$exclude_words = array(“a”,“an”,“as”,“and”,“at”,“but”,“by”,“for”,“in”,“nor”,“of”,“on”,“or”,“so”,“the”,“to”,“it”,“up”,“yet”); // Exclude analyzing these words
$capital_words = array(“asap”,“aku”,“ajk”,“fjmc”,“uhs”,“pmdc”,“ibcc”,“mcat”,“mcq”,“mcqs”,“cmh”,“fumc”,“amc”,“kips”,“etea”,“nust”,“dimc”,“igcse”,“fmc”,“cpmc”,“hec”,“mbbs”,“ptap”,“sat”,“bds”,“kpk”,“fbise”,“lmdc”,“rmc”,“kemu”,“kemc”,“aimc”,“us”,“nts”,“lums”,“sat-ii”,“plab”,“usmle”,“sims”,“skzmdc”,“smh”,“smdc”,“fmh”,“imdc”,“uol”,“nmc”,“cs”,“mc”,“dmc”,“fmdc”,“net”,“imed”,“uk”); // Capital exclusives (leave in lowercase in array)
$max_length = 5; // Maximum word length, anything over is case-lowered
$ending_punc = array(".","!","?");
$retval = $dm->verify_title($data);
$output = array();
$start = 1;
$words = preg_split("/([^a-zA-Z0-9\x27\x26\x22])/", $data, -1,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
foreach($words as $i=>$word)
{
$lcWord = strtolower($word);
$ucWord = strtoupper($word);
if(in_array($lcWord,$capital_words)) $output[$i] = strtoupper($word); // Word was in capital words array
elseif($ucWord==$word) // Word was entered all caps and not in capital words array
{
if(strlen($word)>$max_length) $output[$i] = ucWords($lcWord);
else $output[$i] = $ucWord;
}
elseif($start == 1) $output[$i] = ucWords($lcWord);
elseif(in_array($lcWord,$exclude_words)) $output[$i] = $lcWord;
else $output[$i] = ucWords($lcWord);
if(in_array($word,$ending_punc)) $start = 1;
else if($word != " ") $start = 0;
}
$data = implode($output);
return $retval;
';
}[/PHP]