Improved method of capitalizing first characters of sentences.
The first two manipulations (double spaces & all caps) are optional so can be removed without harm.
<?php
function ucsentence($str) {
if ($str) { $str = preg_replace('/'.chr(32).chr(32).'+/', chr(32), $str); if (($x = substr($str, 0, 10)) && ($x == strtoupper($x))) $str = strtolower($str); $na = array('. ', '! ', '? '); foreach ($na as $n) { if (strpos($str, $n) !== false) { $sa = explode($n, $str); foreach ($sa as $s) $ca[] = ucfirst($s); $str = implode($n, $ca); unset($ca); }
}
return ucfirst(trim($str)); }
}
?>
"heLLo EarthLing!" >> "HeLLo EarthLing!"
"I'M MOSTLY. caps! " >> "I'm mostly. Caps!"
"ALLCAPS" >> "Allcaps"
"i haVe neST.ed punct,u.ation! sp A c es. and CAPs.. " >> "I haVe neST.ed punct,u.ation! Sp A c es. And CAPs.."