note 100848 added to function.mb-convert-case

From: Date: Wed, 10 Nov 2010 21:30:49 +0000
Subject: note 100848 added to function.mb-convert-case
Groups: php.notes 
Request: Send a blank email to php-notes+get-173928@lists.php.net to get a copy of this message
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; } ---- Server IP: 69.147.83.197 Probable Submitter: 74.231.57.2 ---- Manual Page -- http://www.php.net/manual/en/function.mb-convert-case.php Edit -- https://master.php.net/note/edit/100848 Del: integrated -- https://master.php.net/note/delete/100848/integrated Del: useless -- https://master.php.net/note/delete/100848/useless Del: bad code -- https://master.php.net/note/delete/100848/bad+code Del: spam -- https://master.php.net/note/delete/100848/spam Del: non-english -- https://master.php.net/note/delete/100848/non-english Del: in docs -- https://master.php.net/note/delete/100848/in+docs Del: other reasons-- https://master.php.net/note/delete/100848 Reject -- https://master.php.net/note/reject/100848 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173928) next »