Capitalize an English Title

Avalon Internet web development


Automated Capitalization of an English Title

A function to avoid having every word capitalized or only one

by Beryl Magilavy

Titles in English are capitalized according to a set of rules on which educated people agree, despite differing on the details: capitalize the first and all other major words. By supplying Windows with a sloppy title-forming function that capitalizes every single word of a string, Microsoft has done a great disservice to good English writing style. Their default function results in an ugly and incorrect title, leading many writers simply to make the whole title lower-case except for the first word, which is conventional in French, but not English, except for scientific writing.

There are of course rules for how the capitalization should be done in English, and while word lists are required to automate the process, the result is worth the extra time it takes to run the code.

The following function produces a title using the guidelines of the Oxford Manual of Style, capitalizing "the first word and all nouns, pronouns, adjectives, verbs and adverbs, but generally not articles, conjunctions and short prepositions," with the exceptions that this function makes all prepositions lower-case, regardless of length, and also capitalizes the last word. These two exceptions make the style closer to that used in the USA. The function goes through the title, capitalizing every word except those set out in arrays of articles, conjunctions, and prepositions; then goes on to capitalize the first and last words.

There are fine points of grammar not included here, and undoubtedly some words have been mistakenly left out of the lists. The result of using this set of functions will not be as good as if a trained editor did the job, but maybe better than one produced by an average university graduate.

The main function uses a subordinate one that capitalizes only the first letter of a string, useful for writing in French, or to a house style sheet which requires it.

Multibyte considerations are irrelevant to the main function, since it is designed for use with the English language only, which of course has no accented characters. Multibyte string function overrides should be set, or the function itself modified, if the second function is used in UTF-8 for languages other than English. Both functions work at least in PHP4 and PHP5. In the display below, the function's line-lengths will run off the page to the right in windows of reduced size, but can still be copied and pasted.

The Chicago Manual of Style says subordinating prepositions should be capitalized; to do this, take "$conjunction_subordinating," out of the $nocaps merge statement. To avoid capitalization of the last word, eliminate the appropriate line under the comment, "//CAPITALIZE FIRST AND LAST WORDS."

PHP code: Function capitalizing a title in English

function englishTitle($title){

$article=array("a","an","the");

$preposition=array("about","above","across","after","against","along",
"amid","among","around","at","before","behind","below", "beneath",
"beside","besides","between","beyond","but","by","concerning","despite",
"down","during","except","from","in","including", "inside","into","like",
"minus","near","notwithstanding","of","off","on", "onto","opposite","out",
"outside","over","past","per","plus","regarding","since","through",
"throughout","till","to","toward","towards","under","underneath","unless",
"unlike","until","up","upon","versus","via","with","within","without");

$conjunction_coordinating=array("and","but","for","nor","or","so","yet");

$conjunction_subordinating=array("after","although","as","because","if",
"lest","than","that","though","when","whereas","while");

$conjunction_correlative=array("also","both","each","either","neither","whether");

$nocaps=array_merge($article,$preposition,$conjunction_coordinating,
$conjunction_subordinating, $conjunction_correlative);

$multi_word_preposition=array("according to","in addition to","in back of",
"in front of", "in spite of","on top of","other than","together with");

$word=explode(" ",$title);
$first=0;
$n=count($word);$last=$n-1;
	
//CAPITALIZE ONLY WORDS THAT SHOULD BE CAPITALIZED
for($i=0;$i<$n;$i++){
	if(in_array($word[$i],$nocaps)){continue;}
		else{$word[$i]=capitalize($word[$i]);}
	}//END LOOP

	$title=implode(" ",$word);
	
	//REPLACING A STRING THAT MAY HAVE CAPITAL LETTERS
	//WITH ONE THAT IS ALL LOWER-CASE.
	//USE str_ireplace() IF YOU HAVE PHP 5; 
	//IT CAN USE ARRAYS FOR PATTERN AND REPLACEMENT
	foreach($multi_word_preposition as $value){ 
		$title=eregi_replace($value,$value,$title); }
	$word=explode(" ",$title);
	
	//CAPITALIZE FIRST AND LAST WORDS
	$word[$first]=capitalize($word[$first]);
	$word[$last]=capitalize($word[$last]);
	
return implode(" ",$word);	

}//END englishTitle() 


PHP code: Function capitalizing only the first letter of a string

function capitalize($word) {
return strtoupper(substr($word,0 ,1)).substr($word,1);
}
//NO FORCED LOWER CASE, TO PRESERVE, FOR INSTANCE, "McDonald."
//NOTE THE FORCED LOWER CASE IN THE MAIN FUNCTION IS ONLY USED 
//IN MULTI-WORD PREPOSITIONS.

published 4 October, 2006, text revised 18 January, 2007 (code unchanged)