note 16704 added to function.ucfirst

From: Date: Fri, 09 Nov 2001 16:17:38 +0000
Subject: note 16704 added to function.ucfirst
Groups: php.notes 
Request: Send a blank email to php-notes+get-20639@lists.php.net to get a copy of this message
ucfirst() works fine if you only have one sentence to capitalize, but I needed something to sentence-case an entire paragraph. This might not be optimized, but works good enough for me. It does make an attempt to deal with things like "dr. who" (and actually capitalizes the next word after the (Dr./Mr./Mrs./St.) I use this as an override for people who like to post things entirely in UPPERCASE... function SentenceCase($string) { $string = strtolower($string); $LastSentenceEnded = 1; $LastCharWasWhitespace = 1; for ($i=0;$i<strlen($string);$i++) { $thischar = substr($string, $i, 1); if ($LastSentenceEnded && $LastCharWasWhitespace && (ord($thischar) >= 97) && (ord($thischar) <= 122)) { $LastSentenceEnded = 0; $LastCharWasWhitespace = 0; $newstring .= strtoupper($thischar); } else { if (($thischar == " ") || ($thischar == "\n") || ($thischar == "\r") || ($thischar == "\t")) { $LastCharWasWhitespace = 1; } else { $LastCharWasWhitespace = 0; } if (($thischar == ".") || ($thischar == "!")) { $LastSentenceEnded = 1; } $newstring .= $thischar; } } $newstring = str_replace(" dr. ", " Dr.", $newstring); $newstring = str_replace(" mr. ", " Mr.", $newstring); $newstring = str_replace(" mrs. ", " Mrs.", $newstring); $newstring = str_replace(" st. ", " St.", $newstring); return $newstring; } -- http://www.php.net/manual/en/function.ucfirst.php http://master.php.net/manage/user-notes.php?action=edit+16704 http://master.php.net/manage/user-notes.php?action=delete+16704 http://master.php.net/manage/user-notes.php?action=reject+16704

« previous php.notes (#20639) next »