note 80718 added to function.ucwords
| From: | blake at goinoutwest dot com | Date: | Wed, 30 Jan 2008 01:14:27 +0000 |
| Subject: | note 80718 added to function.ucwords | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135574@lists.php.net to get a copy of this message | ||
Relating to the mb_ucwords() function posted by Anonymous. In order for this to actually be
multi-byte compliant, you would also need to use mb_substr() and mb_strlen() instead of substr and
strlen respectively.
Here it is corrected and extended even further to allow multiple word separators and a list of
exceptions to correct after title casing. It's a bit tedious and inelegant, but things
frequently are when dealing with human languages.
function mb_ucwords($str) {
$exceptions = array();
$exceptions['Hp'] = 'HP';
$exceptions['Ibm'] = 'IBM';
$exceptions['Gb'] = 'GB';
$exceptions['Mb'] = 'MB';
$exceptions['Cd'] = 'CD';
$exceptions['Dvd'] = 'DVD';
$exceptions['Usb'] = 'USB';
$exceptions['Mm'] = 'mm';
$exceptions['Cm'] = 'cm';
// etc.
$separator = array(" ","-","+");
$str = mb_strtolower(trim($str));
foreach($separator as $s){
$word = explode($s, $str);
$return = "";
foreach ($word as $val){
$return .= $s . mb_strtoupper($val{0}) . mb_substr($val,1,mb_strlen($val)-1);
}
$str = mb_substr($return, 1);
}
foreach($exceptions as $find=>$replace){
if (mb_strpos($return, $find) !== false){
$return = str_replace($find, $replace, $return);
}
}
return mb_substr($return, 1);
}
----
Server IP: 208.69.120.35
Probable Submitter: 76.83.151.51
----
Manual Page -- http://www.php.net/manual/en/function.ucwords.php
Edit -- https://master.php.net/note/edit/80718
Del: integrated -- https://master.php.net/note/delete/80718/integrated
Del: useless -- https://master.php.net/note/delete/80718/useless
Del: bad code -- https://master.php.net/note/delete/80718/bad+code
Del: spam -- https://master.php.net/note/delete/80718/spam
Del: non-english -- https://master.php.net/note/delete/80718/non-english
Del: in docs -- https://master.php.net/note/delete/80718/in+docs
Del: other reasons-- https://master.php.net/note/delete/80718
Reject -- https://master.php.net/note/reject/80718
Search -- https://master.php.net/manage/user-notes.php