How to convert lowercase string to uppercase using PHP ?
Last Updated :
18 Jul, 2024
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
<?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);
}
}
?>
OutputOriginal 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
<?php
$str = "Geeks^for+Geeks";
print ("Original String \n");
print ($str. "\n");
$cap_str = strtoupper($str);
print ("Uppercase String \n");
print ($cap_str);
?>
OutputOriginal 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
<?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);
?>
OutputOriginal 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
<?php
$string = "hello, world!";
$uppercaseString = preg_replace_callback(
'/[a-z]/',
function ($matches) {
return strtoupper($matches[0]);
},
$string
);
echo $uppercaseString; // Outputs: 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
<?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;
?>
OutputHello 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
<?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";
?>
OutputOriginal string: Hi!GFG User.
Final string: HI!GFG USER.
Similar Reads
How to convert uppercase string to lowercase using PHP ?
Converting an uppercase string to lowercase means changing all capital letters in the string to their corresponding small letters. This is typically done using programming functions or methods to ensure uniformity in text formatting and comparison. Below we have some common approaches Table of Conte
2 min read
How to convert LowerCase values to UpperCase in Input Field using ReactJS ?
To convert the input string from lowercase to uppercase inside an input field using React, we can use the toUpperCase() method. This method easily converts any input string to uppercase. In this article weâll be using the onMouseEnter event listener to trigger the toUpperCase() method when the mouse
2 min read
How to convert the first character to uppercase using PHP ?
A string is a combination of words combined together. The first character of the string can be converted to an upper case in case it is a lower case alphabetic character. Approach 1 : Using chr() method Step 1: The first character of a string can be extracted using the first index, str[0] returns th
3 min read
How to convert a normal string to a uppercase string using filter in VueJS ?
Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is t
2 min read
How to Convert a String to Lower or Upper Case in Ruby?
In this article, we will discuss how to convert a string to lower or upper case in Ruby. We can convert a string to lower or upper case using built-in methods provided in the Ruby language. Table of Content Using DowncaseUsing UpcaseUsing CapitalizeUsing DowncaseThe downcase method is used to conver
2 min read
How to convert first letter of a string to upper case using jQuery ?
The task is to capitalize the first letter of string without using toUpperCase() method with the help of jQuery. There are two approaches that are discussed below: Approach 1: In this example, the css() method is used to set the text-transform property value to capitalize. Example: [GFGTABS] html
2 min read
How to convert first character of all the words uppercase using PHP ?
To convert the first character of all the words present in a string to uppercase, we just need to use one PHP function i.e. ucwords(). ucwords(string,delimiters)This function takes 2 parameters. The first one is the string which is mandatory. The second parameter is the delimiter. Example 1: [GFGTAB
3 min read
How to create half of the string in uppercase and the other half in lowercase?
The first thing that we have to keep in my mind while solving this kind of problem is that the strings are immutable i.e, If I am taking the following string var string1 = "geeksforgeeks";string1[0] = "G";console.log(string1);As strings are immutable, so we cannot change the character of the string,
9 min read
How to convert a string to snake case using JavaScript ?
In this article, we are given a string in and the task is to write a JavaScript code to convert the given string into a snake case and print the modified string. Examples: Input: GeeksForGeeks Output: geeks_for_geeks Input: CamelCaseToSnakeCase Output: camel_case_to_snake_caseThere are some common a
2 min read
PHP Change strings in an array to uppercase
Changing strings in an array to uppercase means converting all the string elements within the array to their uppercase equivalents. This transformation modifies the array so that every string, regardless of its original case, becomes fully capitalized. Examples: Input : arr[] = ("geeks", "For", "GEE
3 min read