note 60064 added to function.ucwords
| From: | php-general at lists dot php dot net | Date: | Sat, 24 Dec 2005 15:34:37 +0000 |
| Subject: | note 60064 added to function.ucwords | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100663@lists.php.net to get a copy of this message | ||
"ieure at php dot net", your idea is pure poetry!
The function below will standardize the capitalization on people's names and the titles of
reports and essays . You may need to adapt the lists in "$all_uppercase" and
"$all_lowercase" to suit the data that you are working with.
function my_ucwords($str, $is_name=false) {
// exceptions to standard case conversion
if ($is_name) {
$all_uppercase = '';
$all_lowercase = 'De La|De Las|Der|Van De|Van Der|Vit De|Von|Or|And';
} else {
// addresses, essay titles ... and anything else
$all_uppercase = 'Po|Rr|Se|Sw|Ne|Nw';
$all_lowercase = 'A|And|As|By|In|Of|Or|To';
}
$prefixes = 'Mc';
$suffixes = "'S";
// captialize all first letters
$str = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")',
strtolower(trim($str)));
if ($all_uppercase) {
// capitalize acronymns and initialisms e.g. PHP
$str = preg_replace("/\\b($all_uppercase)\\b/e",
'strtoupper("$1")', $str);
}
if ($all_lowercase) {
// decapitalize short words e.g. and
if ($is_name) {
// all occurences will be changed to lowercase
$str = preg_replace("/\\b($all_lowercase)\\b/e",
'strtolower("$1")', $str);
} else {
// first and last word will not be changed to lower case (i.e. titles)
$str = preg_replace("/(?<=\\W)($all_lowercase)(?=\\W)/e",
'strtolower("$1")', $str);
}
}
if ($prefixes) {
// capitalize letter after certain name prefixes e.g 'Mc'
$str = preg_replace("/\\b($prefixes)(\\w)/e",
'"$1".strtoupper("$2")', $str);
}
if ($suffixes) {
// decapitalize certain word suffixes e.g. 's
$str = preg_replace("/(\\w)($suffixes)\\b/e",
'"$1".strtolower("$2")', $str);
}
return $str;
}
// A name example
print my_ucwords("MARIE-LOU VAN DER PLANCK-ST.JOHN", true);
// Output: Marie-Lou van der Planc-St.John
// A title example
print my_ucwords("to be or not to be");
// Output: "To Be or Not to Be"
----
Manual Page -- http://www.php.net/manual/en/function.ucwords.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60064
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60064&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60064&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60064&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60064&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60064&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60064&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60064&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60064&report=yes
Search -- http://master.php.net/manage/user-notes.php