Return to Snippet

Revision: 15858
at July 20, 2009 02:16 by teedy


Initial Code
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;
}
$str = "blah blah";
echo strProper($str);

Initial URL
http://us2.php.net/manual/en/function.strtoupper.php

Initial Description

                                

Initial Title
Convert String to Proper Case

Initial Tags

                                

Initial Language
PHP