note 100848 deleted from function.mb-convert-case by danbrown
| From: | danbrown@php.net | Date: | Wed, 10 Nov 2010 21:34:57 +0000 |
| Subject: | note 100848 deleted from function.mb-convert-case by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173931@lists.php.net to get a copy of this message | ||
Note Submitter: arayelcode -at- gmail.com
----
TitleCase and supporting functions created and used to clean up titles of thousands of articles in a
database - all the titles for several years worth of data were stored originally in UPPERCASE. This
is a bit more elaborate than some of the others here, but works well:
function titleCase($str) {
// Each of these sets is incomplete but a good starting point - should be expanded further
$delimiters = array("
",",",".","?","!","-", "(",
")", ":", ";", "\\", "/", "\"",
"'");
$alwaysUpper = array("hiv", "hiv+", "aids", "usa",
"azt", "i", "ii", "iii", "iv",
"v", "fda", "usda", "omb", "dox",
"dna", "rna", "tsi", "nih",
"afb", "rhil", "siv", "rac", "csi");
$alwaysLower = array("a", "at", "as", "be",
"between", "and", "to", "on", "toward",
"towards", "of", "the", "before", "after",
"if", "for",
"with", "within", "without", "in", "into",
"by");
$ret = "";
$str = strtolower($str);
// Split words, keep delimiters (each delimiter is treated as a "word" itself)
$words = preg_split("/([\s,.;:?!\"'\/\-\(\)]+)/", $str, null,
PREG_SPLIT_DELIM_CAPTURE);
$count = count($words);
for ($i = 0; $i < $count; $i++) {
$w = $words[$i];
$len = strlen($w);
// Check all "words" that are at least 2 characters long, and 1 character long
"words" if they are alphabetic
// (mainly to ignore processing case of delimiters as a basic optimization)
if ($len > 1 || ! in_array($w, $delimiters ) ) {
// Is word in our "alwaysUpper[case]" list of exceptions (mainly abbreviations)?
if (in_array($w, $alwaysUpper)) {
$w = strtoupper($w);
} else {
// Is word in our "alwaysLower[case]" list of exceptions (mainly prepositions and
articles)?
if (in_array($w, $alwaysLower)) {
$w = strtolower($w);
} else {
// Most normal words have vowels - if so, use PHP's ucfirst() func to uppercase 1st char
if (hasVowels($w)) {
$w = ucwords($w);
// If word has no vowels, there's a high probability it's an abbreviation - force
upper
} else $w = strtoupper($w);
}
}
}
// "Words" with numbers in them are probably abbreviations (force upper) or actual
numbers
// (Small potential for optimization here)
if (hasNums($w)) $w = strtoupper($w);
// Special case given the medical content we've been parsing in the creation of this
function:
// In our context, words starting with "HIV" (other than the word "hive") are
probably
// derivatives of "HIV" and hence the entire thing is likely an abbreviation (make
upper)
if (stripos(" ".$w, " hiv")!== false && stripos($w,
"hive")===false) $w = strtoupper($w);
// Append "word" (word, delimiter, etc.) to return string
$ret .= $w;
}
// At this point, return string is built - remaining processing relates to "words" with
surrounding
// punctuation or could be extended to include special-case handling for specific phrases
// Special case handling for phrases that would otherwise be mishandled such as
"Hepatitis-a" & "U.S.D.a."
if (strpos($ret, "-")!== false) {
$ret = str_replace("-a", "-A", $ret);
}
if (strpos($ret, ".a.")!== false) {
$ret = str_replace(".a.", ".A.", $ret);
}
return $ret;
}