1.7. Controlling Case1.7.1. ProblemYou need to capitalize, lowercase, or otherwise modify the case of letters in a string. For example, you want to capitalize the initial letters of names but lowercase the rest. 1.7.2. SolutionUse ucfirst( ) or ucwords( ) to capitalize the first letter of one or more words:
Use strtolower( ) or strtoupper( ) to modify the case of entire strings:
1.7.3. DiscussionUse ucfirst( ) to capitalize the first character in a string:
Note that the second line of output is not "1 Monkey face". Use ucwords( ) to capitalize the first character of each word in a string:
As expected, ucwords( ) doesn't capitalize the "t" in "don't." But it also doesn't capitalize the "e" in "76-ers." For ucwords( ), a word is any sequence of nonwhitespace characters that follows one or more whitespace characters. Since both ' and - aren't whitespace characters, ucwords( ) doesn't consider the "t" in "don't" or the "e" in "76-ers" to be word-starting characters. Both ucfirst( ) and ucwords( ) don't change the case of nonfirst letters:
The functions strtolower( ) and strtoupper( ) work on entire strings, not just individual characters. All alphabetic characters are changed to lowercase by strtolower( ) and strtoupper( ) changes all alphabetic characters to uppercase:
When determining upper- and lowercase, these functions respect your locale settings. 1.7.4. See AlsoFor more information about locale settings, see Chapter 16; documentation on ucfirst( ) at http://www.php.net/ucfirst, ucwords( ) at http://www.php.net/ucwords, strtolower( ) at http://www.php.net/strtolower, and strtoupper( ) at http://www.php.net/strtoupper.
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|