note 9581 deleted from ref.strings by maxim
| From: | maxim@php.net | Date: | Tue, 12 Nov 2002 16:16:26 +0000 |
| Subject: | note 9581 deleted from ref.strings by maxim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-39392@lists.php.net to get a copy of this message | ||
Here is a much better more thought out ucsetence() function. I thought about it more and it know
understands .,?! and all other punctuations and allows for making any word or character uppercase.
Just put words to be upper case in array $uppercase. Fixes common user email problems like CAPS,
!!!, and i.
Ex:
$paragraph="I Am AN ItiOT!!! I cAN Not TYPe. WHat IS WRONG with ME??? ME (i) do NOT
knOW....";
$newParagraph=ucsentence($paragraph);
$newParagraph now is:
I am an itiot! I can not type. What is wrong with me? Me (I) do not know.
function ucsentence($paragraph,$uppercase=0) {
//default capitol words
$uppercase[]="I";
//make sentences from $paragraph
$paragraph=strtolower(trim($paragraph));
//replace duplicate chars
$paragraph=ereg_replace('[ ]+'," ",$paragraph);
$paragraph=ereg_replace('[.]+',".",$paragraph);
$paragraph=ereg_replace('[?]+',"?",$paragraph);
$paragraph=ereg_replace('[!]+',"!",$paragraph);
$words=explode(" ",$paragraph);
FOR ($i=0;$i<count($words);$i++) {
//fix sentence capitolization
IF (strstr($words[$i],".")) {
$words[$i+1]=ucfirst($words[$i+1]);
}
IF (strstr($words[$i],"!")) {
$words[$i+1]=ucfirst($words[$i+1]);
}
IF (strstr($words[$i],"?")) {
$words[$i+1]=ucfirst($words[$i+1]);
}
//make array $uppercase uppercase
IF ($uppercase) {
IF
(in_array(strtoupper(ereg_replace('[!@#\$%^&*()_+-=,.?\'\";:]',"",$words[$i])),$uppercase))
{
$words[$i]=strtoupper($words[$i]);
}
}
}
$new_paragraph=ucfirst(trim(implode(" ",$words)));
return $new_paragraph;
} //--end ucsentence--