Voting

: seven minus four?
(Example: nine)

The Note You're Voting On

blake at goinoutwest dot com
17 years ago
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);
}

<< Back to user notes page

To Top