How to Make First Letter Uppercase(Capitalize) of Each Word in a String in PHP?

Problem:

You want to make capitalize first letter of every word in a string.

Solution:

Use built-in function ucwords() to make first character of each word in a string uppercase.

See the following example-

[php]
<?php
$string = "In theory, theory and practice are the same. In practice, they are not.";
echo "Original string: ". $string. "<br />";
$string = ucwords($string);
echo "Converted string: ". $string;
?>
[/php]

[wpdm_file id=144]

Output:
Original string: In theory, theory and practice are the same. In practice, they are not.
Converted string: In Theory, Theory And Practice Are The Same. In Practice, They Are Not.

Leave a Reply

Your email address will not be published. Required fields are marked *