Monday, 13 August 2018

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-
1
2
3
4
5
6
<?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;
?>
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.

0 comments:

Post a Comment