Its prototype follows:
CHAPTER 9 ?– S TRINGS AND REGULAR EXPRESS IONS 253
string strtoupper(string str)
Nonalphabetical characters are not affected. This example uses strtoupper() to
convert a string to all uppercase letters:
$msg = "I annoy people by capitalizing e-mail text.";
echo strtoupper($msg);
?>
This returns the following:
I ANNOY PEOPLE BY CAPITALIZING E-MAIL TEXT.
Capitalizing the First Letter of a String
The ucfirst() function capitalizes the first letter of the string str, if it is alphabetical.
Its prototype follows:
string ucfirst(string str)
Nonalphabetical characters will not be affected. Additionally, any capitalized
characters found in the string will be left untouched. Consider this example:
$sentence = "the newest version of PHP was released today!";
echo ucfirst($sentence);
?>
This returns the following:
The newest version of PHP was released today!
Note that while the first letter is indeed capitalized, the capitalized word PHP was left
untouched.
254 CHAPTER 9 ?– STRINGS AND REGULAR EXPRESSIONS
Capitalizing Each Word in a String
The ucwords() function capitalizes the first letter of each word in a string. Its prototype
follows:
string ucwords(string str)
Nonalphabetical characters are not affected. This example uses ucwords() to capitalize
each word in a string:
$title = "O'Malley wins the heavyweight championship!";
echo ucwords($title);
?>
This returns the following:
O'Malley Wins The Heavyweight Championship!
Note that if O??™Malley was accidentally written as O??™malley, ucwords() would not
catch the error, as it considers a word to be defined as a string of characters separated
from other entities in the string by a blank space on each side.
Pages:
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331