- How to change case in PHP strings to upper, lower, sentence, etc
- Converting a string to lowercase in PHP
- Converting a string to uppercase in PHP
- Converting the first character in a string to uppercase in PHP
- Converting the first character of each word to uppercase in PHP
- Converting the string to sentence case in PHP
- Conclusion
- Related Articles
- Php first to upper case
- PHP ucfirst
- Introduction to the PHP ucfirst() function
- PHP ucfirst() function example
- Dealing with multibyte characters
- Summary
- PHP ucfirst() Function
- Syntax
- Parameter Values
- Technical Details
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
How to change case in PHP strings to upper, lower, sentence, etc
As a developer, you work with strings of different nature in day-to-day projects. Some may be a word long, a phrase, a sentence, or even comprising thousands of words.
It is easy to work on and change the case on shorter strings manually, eg. you can edit the string and make a sentence begin with an uppercase letter, to change an all uppercase text to a sentence case, etc.
In the case of long strings of texts, this will not be an easy task to accomplish and may take too long. The good news is that most programming languages have a way to easily convert and manipulate the case in a string.
In this article, you will learn how to convert strings to different cases using PHP language.
Converting a string to lowercase in PHP
To change all the alphabetical characters in a string to lowercase, we use the PHP strtolower() function.
Converting a string to uppercase in PHP
To change all the alphabetical characters in a string to uppercase, we use the PHP strtoupper() function.
Converting the first character in a string to uppercase in PHP
To change the first alphabetical characters of a string to uppercase, we use ucfirst() function.
Converting the first character of each word to uppercase in PHP
To change the first character of every word in a string in PHP, we use ucwords() function.
Converting the string to sentence case in PHP
All sentences start with an uppercase(capital) letter and end with either a full stop (.), a question mark (?), or an exclamation mark (!).
If our string comprises only one sentence, then converting it to a sentence case is very simple as we only use the ucfirst() function to convert its first character to uppercase.
Which programming language do you love most?
However, that will not be the case with a string comprising of multiple sentences.
— We split the string into an array of sentences using the preg_split() function;
— We loop through all the sentences in a for each loop;
— Convert the string characters in each sentence to lower case using the strtolower() function;
— Convert the first character of every sentence to uppercase using the ucfirst() function;
— Concatenate all our sentences to form back the original string, now with a sentence case.
Hello world! I am a programmer. I like programming in php.
From the above example, you can skip the conversion to lower case if you just want all the sentences to start with an uppercase without affecting the cases of other words/characters.
Conclusion
In this article, you have learned how to convert PHP strings to upper case, lower case, the first character of every word to upper case, the first character of the string to upper case, and also how to convert a combination of multiple sentences into sentence case, where every first character of every sentence starts with an upper case.
It is my hope that the article was simple enough to follow along and easy to understand and implement.
To get notified when we add more incredible content to our site, feel free to subscribe to our free email newsletter.
Related Articles
Php first to upper case
- Different ways to write a PHP code
- How to write comments in PHP ?
- Introduction to Codeignitor (PHP)
- How to echo HTML in PHP ?
- Error handling in PHP
- How to show All Errors in PHP ?
- How to Start and Stop a Timer in PHP ?
- How to create default function parameter in PHP?
- How to check if mod_rewrite is enabled in PHP ?
- Web Scraping in PHP Using Simple HTML DOM Parser
- How to pass form variables from one page to other page in PHP ?
- How to display logged in user information in PHP ?
- How to find out where a function is defined using PHP ?
- How to Get $_POST from multiple check-boxes ?
- How to Secure hash and salt for PHP passwords ?
- Program to Insert new item in array on any position in PHP
- PHP append one array to another
- How to delete an Element From an Array in PHP ?
- How to print all the values of an array in PHP ?
- How to perform Array Delete by Value Not Key in PHP ?
- Removing Array Element and Re-Indexing in PHP
- How to count all array elements in PHP ?
- How to insert an item at the beginning of an array in PHP ?
- PHP Check if two arrays contain same elements
- Merge two arrays keeping original keys in PHP
- PHP program to find the maximum and the minimum in array
- How to check a key exists in an array in PHP ?
- PHP | Second most frequent element in an array
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
PHP ucfirst
Summary: in this tutorial, you’ll learn how to use the PHP ucfirst() function to make the first alphabetic character uppercase.
Introduction to the PHP ucfirst() function
The ucfirst() function accepts a string and returns a new string with the first character converted to uppercase if that character is alphabetic.
Here’s the syntax of the ucfirst() function:
ucfirst ( string $string ) : string
Code language: PHP (php)
The ucfirst() function use the current locale to determine which character is alphabetic.
PHP ucfirst() function example
The following example uses the ucfirst() function to convert the first characters of the first name and last name to uppercase:
$first_name = 'john'; $last_name = 'doe' echo ucfirst($first_name) . ' ' . ucfirst($last_name);
Code language: PHP (php)
John Doe
Code language: PHP (php)
Dealing with multibyte characters
The ucfirst() doesn’t support multibyte strings. To return a new string with the first character converted to uppercase, you can use the following mb_ucfirst() function:
function mb_ucfirst($str) < return mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1); >
Code language: PHP (php)
First, get the first character of the $str using the mb_substr() function and convert it to uppercase using the mb_strtoupper() function:
mb_strtoupper(mb_substr($str, 0, 1))
Code language: PHP (php)
Then, concatenate the uppercase character with the remaining characters in the string:
mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1);
Code language: PHP (php)
The following example uses the ucfirst() to convert the first letter of the string äbtissin to uppercase:
$title = 'äbtissin'; echo ucfirst($title); // doesn't work
Code language: PHP (php)
äbtissin
Code language: PHP (php)
However, it doesn’t work as expected. Because in the default “C” locale characters, the ucfirst() function doesn’t convert character like umlaut-a ( ä ).
The following uses the mb_ucfirst() function instead:
$title = 'äbtissin'; echo mb_ucfirst($title);
Code language: PHP (php)
Äbtissin
Code language: PHP (php)
Summary
- Use the ucfirst() function to returns a string with the first alphabetic character converted to uppercase.
PHP ucfirst() Function
The ucfirst() function converts the first character of a string to uppercase.
- lcfirst() — converts the first character of a string to lowercase
- ucwords() — converts the first character of each word in a string to uppercase
- strtoupper() — converts a string to uppercase
- strtolower() — converts a string to lowercase
Syntax
Parameter Values
Technical Details
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.