note 64379 added to function.mb-convert-case

From: Date: Thu, 13 Apr 2006 05:40:03 +0000
Subject: note 64379 added to function.mb-convert-case
Groups: php.notes 
Request: Send a blank email to php-notes+get-107956@lists.php.net to get a copy of this message
Automated Correct Capitalization of a Title in English - Can it be that we can invent Unicode, but cannot properly write a title in English? The following function pair will do it to a level at least of an average fluent speaker's English skills. It doesn't manage details such as various uses of the same word (there are contexts in which "that" should be capitalized), and I am sure the grammar contains errors and omissions. (Drop me a note and I will edit this post.) Functions explode() and implode() appear to work under UTF-8, but I am not confident of my testing conditions. Capitalize() and englishTitle() work outside the multi-byte context with the "mb_" removed where it appears on the PHP functions. This would be more elegant as an object, but would perhaps be less useful to some users. <?php function capitalize($word) {//NO FORCED LOWER CASE SO AS NOT TO DISTURB PROPER NOUNS, SUCH AS McDonald return mb_strtoupper(mb_substr($word,0 ,1)).mb_substr($word,1); } function englishTitle($title){ $article=array("a", "an", "the"); $preposition=array("about", "above", "across", "after", "against", "along", "amid", "among", "around", "at", "before", "behind", "below", "beneath", "beside", "besides", "between", "beyond", "but", "by", "concerning", "despite", "down", "during", "except", "from", "in", "including", "inside", "into", "like", "minus", "near", "notwithstanding", "of", "off", "on", "onto", "opposite", "out", "outside", "over", "past", "per", "plus", "regarding", "since", "through", "throughout", "till", "to", "toward", "towards", "under", "underneath", "unless", "unlike", "until", "up", " upon", "versus", "via", "with", "within", "without"); $conjunction_coordinating=array("and", "but", "for", "nor", "or", "so", "yet"); $conjunction_subordinating=array("after", "although", "as", "because", "if", "lest", "than", "that", "though", "when", "whereas", "while"); $conjunction_correlative=array("also", "both", "each", "either", "neither", "whether"); $nocaps=array_merge($article, $preposition, $conjunction_coordinating, $conjunction_subordinating, $conjunction_correlative); $multi_word_preposition=array("according to", "in addition to", "in back of", "in front of", "in spite of", "on top of", "other than", "together with"); $word=explode(" ",$title); $first=0; $n=count($word);$last=$n-1; //CAPITALIZE ONLY WORDS THAT SHOULD BE CAPITALIZED for($i=0;$i<$n;$i++){ if(in_array($word[$i],$nocaps)){continue;} else{$word[$i]=capitalize($word[$i]);} } $title=implode(" ",$word); //REPLACING A STRING THAT MAY HAVE CAPITAL LETTERS WITH ONE THAT IS ALL LOWER-CASE //USE str_ireplace() IF YOU HAVE PHP 5; IT IS FASTER AND CAN USE ARRAYS FOR PATTERN AND REPLACEMENT foreach($multi_word_preposition as $value){ $title=mb_eregi_replace($value,$value,$title); } $word=explode(" ",$title); //CAPITALIZE FIRST AND LAST WORDS $word[$first]=capitalize($word[$first]); $word[$last]=capitalize($word[$last]); return implode(" ",$word); }//END englishTitle() ---- Server IP: 64.71.164.2 Probable Submitter: 68.127.106.183 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.mb-convert-case.php Edit -- http://master.php.net/note/edit/64379 Del: integrated -- http://master.php.net/note/delete/64379/integrated Del: useless -- http://master.php.net/note/delete/64379/useless Del: bad code -- http://master.php.net/note/delete/64379/bad+code Del: spam -- http://master.php.net/note/delete/64379/spam Del: non-english -- http://master.php.net/note/delete/64379/non-english Del: in docs -- http://master.php.net/note/delete/64379/in+docs Del: other reasons-- http://master.php.net/note/delete/64379 Reject -- http://master.php.net/note/reject/64379 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#107956) next »