PHP String - ucwords() Function
The PHP ucwords() function returns a string with the first character of each word in string capitalized, if that character is alphabetic. For this function, a word is a string of characters that are not listed in the separators parameter. By default, these are: space, horizontal tab, carriage return, newline, form-feed and vertical tab.
Note: This function is binary-safe.
Syntax
ucwords(string, separators)
Parameters
string |
Required. Specify the string to convert. |
separators |
Optional. Specify separators containing the word separator characters. Default is " \t\r\n\f\v". |
Return Value
Returns the modified string.
Example:
The example below shoes the usage of ucwords() function.
<?php $str1 = "hello world!"; echo ucwords($str1)."\n"; $str2 = "HELLO WORLD!"; echo ucwords($str2)."\n"; echo ucwords(strtolower($str2))."\n"; ?>
The output of the above code will be:
Hello World! HELLO WORLD! Hello World!
Example:
Consider one more example where custom separators are used with ucwords() function.
<?php //"|" as separator $str1 = "hello|world!"; echo ucwords($str1, "|")."\n"; //adding ' as separator along //with default separators $str2 = "o'reilly media"; echo ucwords($str2, " \t\r\n\f\v'")."\n"; ?>
The output of the above code will be:
Hello|World! O'Reilly Media
❮ PHP String Reference