Building upon Justin's and Alex's work...
This function allows you to specify which delimiter(s) to explode on (not just the default space). Now you can correctly capitalize Irish names and hyphenated words (if you want)!
<?php
function titleCase($string, $delimiters = array(" ", "-", "O'"), $exceptions = array("to", "a", "the", "of", "by", "and", "with", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X")) {
foreach ($delimiters as $delimiter){
$words = explode($delimiter, $string);
$newwords = array();
foreach ($words as $word){
if (in_array(strtoupper($word), $exceptions)){
$word = strtoupper($word);
} elseif (!in_array($word, $exceptions)){
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}
return $string;
}
?>