note 112795 added to function.ucwords
| From: | antoniomax at antoniomax dot com | Date: | Tue, 23 Jul 2013 18:04:42 +0000 |
| Subject: | note 112795 added to function.ucwords | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195739@lists.php.net to get a copy of this message | ||
Para formatar nomes em pt-br:
<?php
function titleCase($string, $delimiters = array(" ", "-", ".",
"'", "O'", "Mc"), $exceptions = array("de",
"da", "dos", "das", "do", "I", "II",
"III", "IV", "V", "VI"))
{
/*
* 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;
}
?>
Usage:
<?php
$s = 'SÃO JOÃO DOS SANTOS';
$v = titleCase($s); // 'São João dos Santos'
?>
----
Server IP: 69.147.83.199
Probable Submitter: 189.34.104.235
----
Manual Page -- http://php.net/manual/en/function.ucwords.php
Edit -- https://master.php.net/note/edit/112795
Del: integrated -- https://master.php.net/note/delete/112795/integrated
Del: useless -- https://master.php.net/note/delete/112795/useless
Del: bad code -- https://master.php.net/note/delete/112795/bad+code
Del: spam -- https://master.php.net/note/delete/112795/spam
Del: non-english -- https://master.php.net/note/delete/112795/non-english
Del: in docs -- https://master.php.net/note/delete/112795/in+docs
Del: other reasons-- https://master.php.net/note/delete/112795
Reject -- https://master.php.net/note/reject/112795
Search -- https://master.php.net/manage/user-notes.php