Open In App

How to convert lowercase string to uppercase using PHP ?

Last Updated : 18 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

A string is a storage object in PHP comprising characters, numbers or special symbols. Strings in PHP are case-sensitive. The interconversion between lower and upper case can be easily done using various in-built methods in PHP.

Using chr() method

This approach uses a variety of in-built PHP methods to convert a string to upper case characters. Initially, a string is declared consisting of characters, numbers or special symbols. The str_split() method is used to convert the string into an array of individual characters. The characters are mapped to specific indexes together to form a character array. 

A for loop iteration is then performed over this array. Each character is validated, to verify if is a lower case alphabetic character. The ctype_lower() method is used to return a boolean value depending on which category the character belongs. This method returns TRUE, if the specified argument character is in lower case, else returns FALSE.

ctype_lower(ch)

In case, this method returns FALSE, the character is either a non-alphabetic character or an upper case character. If this scenario arises, the character is displayed unmodified. Else, the particular character is converted to upper case character, by subtracting ’32’ from its ASCII value. 

ord (ch ) - 32

This integer value, is then converted to its corresponding character value using the chr() method. 

The time complexity required for this approach’s execution is O(l), where l is the length of the string. 

<?php
  
 // Declare string
$str = "Geeks^for+Geeks";
print ("Original String \n");

// Split string in characters
$chars = str_split($str);
print ($str. "\n");
print ("Uppercase String \n");

// Looping over characters
foreach ($chars as $ch){
  
  // Check if character is 
  // small case alphabet
  if(ctype_lower($ch)){
    
     // Convert to upper case
       echo chr(ord($ch)-32);
  }
  else{
    
    // Else print character
    // unmodified
    echo($ch);
  }
}
?>

Output
Original String 
Geeks^for+Geeks
Uppercase String 
GEEKS^FOR+GEEKS

Using strtoupper() method

The strtoupper() is an inbuilt method in PHP which carries out case-conversion. The alphabetics are all converted to upper case, and returned to the form of a string. The numbers and special symbols remain unmodified. The result has to be saved in a variable in order to preserve the changes. This method is faster and optimized in comparison to the previous approach.

strtoupper ( $string ) 

Return Type : 

Returns a string with all upper case characters. 

<?php
$str = "Geeks^for+Geeks";
print ("Original String \n");
print ($str. "\n");
$cap_str = strtoupper($str);
print ("Uppercase String \n");
print ($cap_str);

?>

Output
Original String 
Geeks^for+Geeks
Uppercase String 
GEEKS^FOR+GEEKS

Using mb_convert_case() function

The mb_convert_case() function in PHP is used for case folding, which means converting all characters of a string to uppercase or lowercase based on the specified mode.

Example:

<?php
$str = "geeks^for+geeks";
print ("Original String \n");
print ($str. "\n");

// Convert string to uppercase using mb_convert_case
$upperStr = mb_convert_case($str, MB_CASE_UPPER);
print ("Uppercase String \n");
print ($upperStr);
?>

Output
Original String 
geeks^for+geeks
Uppercase String 
GEEKS^FOR+GEEKS

Using preg_replace_callback()

The preg_replace_callback() function allows you to perform a callback on each match found using a regular expression. This can be used to convert each character to uppercase.

Example:

<?php
$string = "hello, world!";
$uppercaseString = preg_replace_callback(
    '/[a-z]/', 
    function ($matches) {
        return strtoupper($matches[0]);
    }, 
    $string
);
echo $uppercaseString; // Outputs: HELLO, WORLD!
?>

Output
HELLO, WORLD!

Using ucwords() Method

The ucwords() function in PHP is used to convert the first character of each word in a string to uppercase. This can be useful for title casing a string where each word starts with an uppercase letter.

Example: This method is particularly useful when you need to capitalize the first letter of each word in a string, providing a different approach to case conversion in PHP.

<?php
// Declare a string
$string = "hello world! welcome to php.";

// Convert the first character of each word to uppercase
$convertedString = ucwords($string);

// Output the result
echo $convertedString;
?>

Output
Hello World! Welcome To Php.

Using array_map and strtoupper

Use array_map with strtoupper to convert a string to uppercase in PHP. First, split the string into an array with str_split. Apply strtoupper to each element using array_map, and then combine the array back into a string with implode.

Example:

<?php
// Original string
$str = "Hi!GFG User.";
echo "Original string: " . $str . "\n";

// Using array_map and strtoupper
$array = str_split($str);
$upperArray = array_map('strtoupper', $array);
$final_str = implode('', $upperArray);

echo "Final string: " . $final_str . "\n";
?>

Output
Original string: Hi!GFG User.
Final string: HI!GFG USER.


Next Article

Similar Reads

three90RightbarBannerImg