note 64379 deleted from function.mb-convert-case by bjori
| From: | bjori@php.net | Date: | Thu, 13 Apr 2006 11:18:56 +0000 |
| Subject: | note 64379 deleted from function.mb-convert-case by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-107970@lists.php.net to get a copy of this message | ||
Note Submitter: bmagilavy at avalon-internet dot com
Reason: useless
----
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()