note 16704 deleted from function.ucfirst by jimw

From: Date: Sat, 10 Nov 2001 23:35:52 +0000
Subject: note 16704 deleted from function.ucfirst by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-20713@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; }

« previous php.notes (#20713) next »