Version: 1.0
Type: Full Script
Category: Algorithms
License: GNU General Public License
Description: This dandy function uc_sentences that I made will capitalize the first word of every sentence. It’s very nice as opposed to uc_words which looks tacky and is hard to read. Hopefully the PHP gods will put this into the next build they conjure up. My email is [email protected] if you have any questions or comments. Thanks!
<? function uc_sentences($sString) { $sString = strtolower($sString); $words = split(" ", $sString); // each entry in array $words is a word from the string $firstword = false; $sNewString = ""; foreach($words AS $wordkey=>$word) { $word = trim($word); // just in case people double-space between sentences $lastchar = substr($word, -1); // $lastchar is used to determine if end-of-sentence is here if(($firstword) OR ($wordkey==0)) // if it's the start of sentence then we capitalize the word { $word = ucfirst($word); $firstword = false; $sNewString = $sNewString . " $word"; // add the word to the output string } elseif(($lastchar==".") OR($lastchar=="!") OR ($lastchar=="?")) // you can add more chars if you need to { $firstword = true; // now the next word will be first word of new sentence $sNewString = $sNewString . " $word"; // add the word to the output string } else { $sNewString = $sNewString . " $word"; // add the word to the output string } } $sNewString = trim($sNewString); // sometimes an extra space at beginning or end occurs, so this fixes it return $sNewString; // return the new string } // call it like this $badstring = "I LIKE TO EAT POTATO CHIPS. GO EAT POTATO CHIPS! WHERE ARE THE CHIPS?"; // example string print uc_sentences($badstring); // this will print "I like to eat potato chips. Go eat potato chips. Where are the potato chips?" ?>