note 29060 deleted from function.ucwords by maxim
| From: | maxim@php.net | Date: | Fri, 31 Jan 2003 21:23:57 +0000 |
| Subject: | note 29060 deleted from function.ucwords by maxim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-43225@lists.php.net to get a copy of this message | ||
I needed something to do smart capitalization of names entered by users. While looking through
thousands of user-entered names, I realized that there were three types of people: those that typed
in all lower-case, all upper-case, or mixed case.
examples:
john smith
JANE DOE
Jeanne de Boer
Bobby-Jo MacDonald
I realized that people with intercapitalization in their names tended to type in mixed-case, so I
made a function to do capitalization to names that were either all lower-case or all upper-case, and
to leave mixed-case alone. I believe that this takes care of many internationalization issues in a
subtle, intuitive, and user-empowering manner.
Call it like this:
print my_ucwords ( 'BRYON THUR' );
print my_ucwords ( 'chris steinberg' );
print my_ucwords ( 'Mike van Lammeren' );
function my_ucwords( $given_string ) {
if( preg_match( '/[A-Z]/', $given_string ) and preg_match( '/[a-z]/',
$given_string ) ) {
# mixed-case detected, so don't touch string
return $given_string;
}
return ucwords( strtolower( $given_string ) );
}