How to change case in PHP strings to upper, lower, sentence, etc
  John Mwaniki /   29 Sep 2021

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.

Example:

<?php
$string = "Hello World!";
echo strtolower($string);
?>

Output:

hello world!

 

Converting a string to uppercase in PHP

To change all the alphabetical characters in a string to uppercase, we use the PHP strtoupper() function.

Example:

<?php
$string = "Hello World!";
echo strtoupper($string);
?>

Output:

HELLO WORLD!

 

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.

Example:

<?php
$string = "hello world!";
echo ucfirst($string);
?>

Output:

Hello world!

 

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.

Example:

<?php
$string = "hello world!";
echo ucwords($string);
?>

Output:

Hello World!

 

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.

Example:

<?php
$string = "which programming language do you love most?";
echo ucfirst($string);
?>

Output:

Which programming language do you love most?

However, that will not be the case with a string comprising of multiple sentences.

In such a case:

- 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.

Example:

<?php
$string = "hello world! i am a programmer. i like programming in php.";
$sentences = preg_split('/([.?!]+)/', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); 

$newstring = ""; 
foreach ($sentences as $sentence) { 
 $newsentence = ucfirst(strtolower(trim($sentence)));
 /*
 The above line of code trims white space at the beginning and end of the sentence.. trim()
 Converts it to lower case using strtolower()
 Then converts the first character to upper case using ucfirst()
 Then assigns it to the variable $newsentence
 */
 $newstring .= $newsentence; // Concatenate to the $newstring
 if(preg_match('/([.?!]+)/', $sentence)){
  $newstring .= " "; // Add a space after fullstop, question mark, or exclamation mark
 }
} 
echo $newstring;
?>

Output:

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.