I have a large number of strings to process in php. I want to ‘fix’ them to be title case (using ucwords(strtolower($str))) but only if they are all upper or all lower case already. If they are already mixed case, I’d just rather just leave them as they are.
What is the fastest way to check for this? It seems like foring through the string would be a rather slow way to go about it.
Here’s what I have, which I think will be too slow:
function fixCase($str) { $uc = 0; $lc = 0; for($i=0;$i<strlen($str);$i++) { if ($str[$i] >= 'a' && $str[$i] <= 'z') $lc++; else if ($str[$i] >= 'A' && $str[$i] <= 'Z') $uc++; } if ($uc == 0 || $lc == 0) { return ucwords(strtolower($str)); } }
just use a string compare (case sensitive)