note 68340 deleted from function.strtoupper by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:06:57 +0000 |
| Subject: | note 68340 deleted from function.strtoupper by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189705@lists.php.net to get a copy of this message | ||
Note Submitter: Cory
----
This function converts any series of english words to Proper Casing. It also accounts for words
such as 'a' and 'the'. To change what words are ignored, just change the $noUp
array.
function strProper($str) {
$noUp =
array('a','an','of','the','are','at','in');
$str = trim($str);
$str = strtoupper($str[0]) . strtolower(substr($str, 1));
for($i=1; $i<strlen($str)-1; ++$i) {
if($str[$i]==' ') {
for($j=$i+1; $j<strlen($str) && $str[$j]!=' '; ++$j); //find next space
$size = $j-$i-1;
$shortWord = false;
if($size<=3) {
$theWord = substr($str,$i+1,$size);
for($j=0; $j<count($noUp) && !$shortWord; ++$j)
if($theWord==$noUp[$j])
$shortWord = true;
}
if( !$shortWord )
$str = substr($str, 0, $i+1) . strtoupper($str[$i+1]) . substr($str, $i+2);
}
$i+=$size;
}
return $str;
}