Capitalize first letters of a string with php
- 0
How can I make the first letters of each word in a string uppercase in PHP?
Answers
- 0
You can use the ucwords() function which is a built-in PHP function that converts the first character of each word in a string to uppercase and the rest to lowercase. It’s similar to ucfirst(), which only capitalizes the first character of the first word in a string.
function capitalizeWords($string) {
return ucwords($string);
}
Then use the function like this:
$string = "hello world";
$capitalizedString = capitalizeWords($string);
echo $capitalizedString; // Outputs: "Hello World"