PHP Reference ยท String Functions
PHP ucwords() function
Reference for PHP ucwords(): uppercase the first letter of each word, change the delimiter list for hyphens and underscores, and handle mixed-case input.
ucwords() returns a string in which the first letter of every word is uppercased. By default a "word" starts after a space, a tab, a carriage return, a newline, a form feed or a vertical tab. The remaining letters are not changed.
You can pass your own list of delimiter characters, which makes it useful for hyphenated names or snake_case identifiers.
<?php echo ucwords("the quick brown fox"); // The Quick Brown Fox
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
ucwords(string $string, string $separators = " \t\r\n\f\v"): string
| Name | Description |
|---|---|
$string |
The input string. |
$separators |
Optional. Every character in this string counts as a word separator. The character after a separator is uppercased. |
Return value: the string with the first letter of each word uppercased (ASCII letters only).
Usage notes
- Only the first letter of each word is changed. Combine with
strtolower()to turn"jOHN sMITH"intoJohn Smith. - Custom
$separatorsreplace the default list. If you pass"-", spaces no longer separate words; pass" -"to get both. - Hyphens and underscores are not separators by default, which is why
"hello-world"staysHello-world. - ASCII only. Since PHP 8.2 the function no longer depends on the locale. For Unicode text use
mb_convert_case($s, MB_CASE_TITLE). - Not real title case. Small words ("of", "the") are capitalized too, and names such as
McDonaldare not handled. - Related single-letter functions:
ucfirst()for the start of the string only,lcfirst()for the opposite.
Related
ucfirst(),lcfirst(),strtolower(),mb_strtoupper(),wordwrap()- Lessons: Strings, String functions