Saturday, August 16, 2008

ucsentence: Using PHP to Capitalize Sentences, Paragraphs, Text

Most PHP developers are familiar with the "ucfirst()" and "ucwords()" capitalization functions. The ucfirst() function accepts a string as an argument, returning the string with the first character capitalized. The ucwords() function accepts a string as an argument, returning the string with the first letter of each word capitalized. These are very useful functions, but what if you have a string that contains several sentences, or a paragraph, and you want to properly capitalize the first letter of each sentence? This is where ucsentence() comes in.

This PHP function accepts a string of text that contains one or more sentences, each ending in one of the following ., !, or ? returning properly capitalize text. It is extremely handy when you have a lot of text that is either all lowercase or all uppercase that needs normalizing.

function ucsentence($string)
{
if(strlen($string) < 3)
return $string;

$string = strtolower($string);
foreach(array('.', '?', '!') AS $punct)
{
$string = explode($punct, $string);
$count = count ($string);
for($i = 0; $i < $count; $i++)
{
$string[$i] = ucfirst (trim ($string[$i]));
if($i > 0)
{
if((ord($string[$i]{0}) < 48) || (ord($string[$i]{0}) > 57))
$string[$i] = ' '.$string[$i];
}
}
$string = implode($punct, $string);
}
return $string;
}

USAGE:
$paragraph = "THIS IS ANNOYING TEXT. someone doesn't know how to punctuate.";
print ussentence($paragraph);

This function is an enhanced version of one found in the ucwords() section of the PHP documentation comments, extended to test for multiple punctuation types.

No comments: