I care a lot about capitalization (probably too much). So I wrote a function that fixes the capitalization everywhere on my site. I basically want "title case" but with some exceptions... words I don't like to see capitalized and acronyms.
function my_capitals($string)
{
$uc = ucwords($string);
$tokens = explode(' ',$uc);
foreach ($tokens as $key=>$val)
{
if ($val == 'Ipa') $tokens[$key] = 'IPA';
else if ($val == 'Ipas') $tokens[$key] = 'IPAs';
else if ($val == 'Apa') $tokens[$key] = 'APA';
else if ($val == 'Apas') $tokens[$key] = 'APAs';
else if ($val == 'A') $tokens[$key] = 'a';
else if ($val == 'And') $tokens[$key] = 'and';
else if ($val == 'The') $tokens[$key] = 'the';
else if ($val == 'In') $tokens[$key] = 'in';
else if ($val == 'Or') $tokens[$key] = 'or';
else if ($val == 'Of') $tokens[$key] = 'of';
else if ($val == 'To') $tokens[$key] = 'to';
else if ($val == 'On') $tokens[$key] = 'on';
else if ($val == 'At') $tokens[$key] = 'at';
else $tokens[$key] = $val;
}
$final = implode(' ',$tokens);
return $final;
}
Imagine there might be another 10-15 options and that it may be run 3-5 times per page on relatively short strings (one-line descriptions and titles).
My question is this: Is this an efficient way to accomplish this sort of translation? Or should I be figuring out a more efficient way to do it? Is there another alternative I don't know of, as opposed to just switch which probably has similar performance?