PHP capitalize first letter
In this post, you will learn how to capitalize the first letter in PHP.
In the development process, there are different situations where we need to make a string of completely uppercase or lowercase or camel case letters. PHP provides many in-build functions to capitalize the first letter.
PHP ucwords()
PHP provides the ucwords() function to convert the first character of each word to uppercase. It has the following syntax -
ucwords(string, delimiters)
Here the string specifies the string to convert. It is the required parameter. It returns a string with the first character of each word of the string capitalized. The second parameter delimiters is optional. It specifies the word separator character.
Example to capitalize the first letter using ucwords()
In the given example, the ucwords() method makes the first letter of each word capital.
<?php
$str = "welcome to etutorialspoint!";
echo ucwords($str);
?>
Output of the above code:
Welcome To Etutorialspoint!
Suppose we have multiple sentences. Then this function will not only capitalize the first letter of each word of first sentence but also for all sentences. The following example demonstrates this -
<?php
$str = "it is very easy to defeat someone. but it is very hard to Win someone.";
echo ucwords($str);
?>
Related Articles
PHP reverse a string without predefined functionPHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP random quote generator
PHP calculate percentage of total
PHP sanitize input for MySQL
Display PDF using an AJAX call
How to fetch data from database in php and display in pdf
How to read CSV file in PHP and store in MySQL
How to create a doc file using PHP
PHP SplFileObject Examples
How to Upload a File in PHP
Sending HTML form data to an email address