note 92317 added to function.mb-convert-case
| From: | agashatfreemaildothu at osu1 dot php dot net | Date: | Sun, 19 Jul 2009 10:27:41 +0000 |
| Subject: | note 92317 added to function.mb-convert-case | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158471@lists.php.net to get a copy of this message | ||
as the previouly posted version of this function doesn't handle UTF-8 characters, I simply
tried to replace ucfirst to mb_convert_case, but then any previous case foldings were lost while
looping through delimiters.
So I decided to do an mb_convert_case on the input string (it also deals with words is uppercase
wich may also be problematic when doing case-sensitive search), and do the rest of checking after
that.
As with mb_convert_case, words are capitalized, I also added lowercase convertion for the
exceptions, but, for the above mentioned reason, I left ucfirst unchanged.
Now it works fine for utf-8 strings as well, except for string delimiters followed by an UTF-8
character ("Mcádám" is unchanged, while "mcdunno's" is converted to
"McDunno's" and "ökrös-TÃTH éDUa" in also put in the correct
form)
I use it for checking user input on names and addresses, so exceptions list contains some hungarian
words too.
<?php
function titleCase($string, $delimiters = array(" ", "-", ".",
"'", "O'", "Mc"), $exceptions = array("út",
"u", "s", "és", "utca", "tér",
"krt", "körút", "sétány", "I", "II",
"III", "IV", "V", "VI", "VII", "VIII",
"IX", "X", "XI", "XII", "XIII", "XIV",
"XV", "XVI", "XVII", "XVIII", "XIX",
"XX", "XXI", "XXII", "XXIII", "XXIV",
"XXV", "XXVI", "XXVII", "XXVIII", "XXIX",
"XXX" )) {
/*
* 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
*/
$string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
foreach ($delimiters as $dlnr => $delimiter){
$words = explode($delimiter, $string);
$newwords = array();
foreach ($words as $wordnr => $word){
if (in_array(mb_strtoupper($word, "UTF-8"), $exceptions)){
// check exceptions list for any words that should be in upper case
$word = mb_strtoupper($word, "UTF-8");
}
elseif (in_array(mb_strtolower($word, "UTF-8"), $exceptions)){
// check exceptions list for any words that should be in upper case
$word = mb_strtolower($word, "UTF-8");
}
elseif (!in_array($word, $exceptions) ){
// convert to uppercase (non-utf8 only)
$word = ucfirst($word);
}
array_push($newwords, $word);
}
$string = join($delimiter, $newwords);
}//foreach
return $string;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 80.99.188.102
----
Manual Page -- http://www.php.net/manual/en/function.mb-convert-case.php
Edit -- https://master.php.net/note/edit/92317
Del: integrated -- https://master.php.net/note/delete/92317/integrated
Del: useless -- https://master.php.net/note/delete/92317/useless
Del: bad code -- https://master.php.net/note/delete/92317/bad+code
Del: spam -- https://master.php.net/note/delete/92317/spam
Del: non-english -- https://master.php.net/note/delete/92317/non-english
Del: in docs -- https://master.php.net/note/delete/92317/in+docs
Del: other reasons-- https://master.php.net/note/delete/92317
Reject -- https://master.php.net/note/reject/92317
Search -- https://master.php.net/manage/user-notes.php