![]() |
VOOZH | about |
To convert the first character of all the words present in a string to uppercase, we just need to use one PHP function i.e. ucwords().
This function takes 2 parameters. The first one is the string which is mandatory. The second parameter is the delimiter.
Example 1:
Welcome To Geeks For Geeks
The second parameter(delimiter) is optional. If we don't mention it, then it will capitalize all the words after space. The echo keyword is used to print the output on the screen.
Example 2:
Hey!Let's get started
As "!" is the delimiter, it will only capitalize the first letter of the word before and after "!".
Example 3:
Php Is Fun Learning
For storing a string in a variable we need to put $ sign in front of the variable.
Using a foreach loop, split the string into words using explode(). Iterate through each word with foreach, capitalize the first character using ucfirst(), then join the words back into a string with implode().
Example: In this example capitalizes the first letter of each word in the string "hello, world!" using explode(), ucfirst(), and implode(), resulting in "Hello, World!".
Hello, World!
In this method, preg_replace_callback is used to match the first character of each word and apply the strtoupper function to capitalize it. This approach gives us more control over the matching process and can be easily customized.
Example:
Hello, World! Let'S Code.
The mb_convert_case() function in PHP is used to perform case conversions on multi-byte strings. It can be used to convert the first character of all the words present in a string to uppercase by utilizing the MB_CASE_TITLE mode.
Example:
Original String: hello, world! welcome to geeks for geeks. Modified String: Hello, World! Welcome To Geeks For Geeks.