PHP 8 reference · String Functions
PHP ucwords() Function
Uppercase the first letter of each word in a string. By default words are separated by spaces, tabs and new lines.
PHP 8 Since 4.0
Syntax
ucwords(string $string, string $separators = " \t\r\n\f\v"): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
separators optional |
Characters that start a new word. Default: " \t\r\n\f\v" |
Return value
The string with each word capitalized.
Examples
<?php
echo ucwords("the old man and the sea"), "\n";
echo ucwords(strtolower("JOHN O'REILLY")), "\n"; // ' isn't a separator
Output
The Old Man And The Sea John O'reilly
<?php
echo ucwords("mary-jane watson", " -"), "\n";
echo ucwords("first_name", "_"), "\n";
Output
Mary-Jane Watson First_Name
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
str.title()· Python
Related
ucfirst()· Make the first character of a string uppercase and leave the rest as it is. Only ASCII letters change.mb_convert_case()· Change the case of a string with Unicode rules: upper, lower, title case or case folding for comparisons.strtolower()· Convert every ASCII letter in a string to lowercase. Since PHP 8.2 it ignores the locale; use mb_strtolower() for é, Ü and others.- PHP Strings · tutorial