note 90165 added to function.mb-convert-case
| From: | theatpsychoticneuroticdotcom at osu1 dot php dot net | Date: | Wed, 08 Apr 2009 17:03:40 +0000 |
| Subject: | note 90165 added to function.mb-convert-case | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152967@lists.php.net to get a copy of this message | ||
Building upon Justin's and Alex's work...
This function allows you to specify which delimiter(s) to explode on (not just the default space).
Now you can correctly capitalize Irish names and hyphenated words (if you want)!
function titleCase($string, $delimiters = array(" ", "-", "o'"),
$exceptions = array("to", "a", "the", "of", "by",
"and", "with", "II", "III", "IV", "V",
"VI", "VII", "VIII", "IX", "X")) {
/*
* Exceptions in lower case are words you don't want converted
* Exceptions all in upper case are any words you don't want converted to title case
* but should be converted to upper case, e.g.:
* king henry viii or king henry Viii should be King Henry VIII
*/
foreach ($delimiters as $delimiter){
$words = explode(strtoupper($delimiter), $string);
$newwords = array();
foreach ($words as $word){
if (in_array(strtoupper($word), $exceptions)){
// check exceptions list for any words that should be in upper case
$word = strtoupper($word);
} elseif (!in_array($word, $exceptions)){
// convert to uppercase
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}
return $string;
}
----
Server IP: 209.41.74.194
Probable Submitter: 144.167.69.80
----
Manual Page -- http://www.php.net/manual/en/function.mb-convert-case.php
Edit -- https://master.php.net/note/edit/90165
Del: integrated -- https://master.php.net/note/delete/90165/integrated
Del: useless -- https://master.php.net/note/delete/90165/useless
Del: bad code -- https://master.php.net/note/delete/90165/bad+code
Del: spam -- https://master.php.net/note/delete/90165/spam
Del: non-english -- https://master.php.net/note/delete/90165/non-english
Del: in docs -- https://master.php.net/note/delete/90165/in+docs
Del: other reasons-- https://master.php.net/note/delete/90165
Reject -- https://master.php.net/note/reject/90165
Search -- https://master.php.net/manage/user-notes.php