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
Method 1: Using strtolower()
The strtolower() function converts all alphabetic characters in a string to lowercase. This function is simple and effective for single-byte character encodings.
Syntax:
string strtolower( $string )
Return value: It returns the string converted into lower string.
Example 1:
<?php
echo strtolower("GeeksForGeeks")
?>
Output
geeksforgeeks
Method 2: Using mb_strtolower() Function
Another efficient way to convert an uppercase string to lowercase in PHP is by using the mb_strtolower() function. This function is particularly useful for strings with multibyte characters (such as those in UTF-8) and ensures proper conversion in various languages.
Syntax:
string mb_strtolower( $string, $encoding = mb_internal_encoding() )
Example:
<?php
$uppercaseString = "GEEKS";
$lowercaseString = mb_strtolower($uppercaseString, 'UTF-8');
echo $lowercaseString;
?>
Output
geeks
array_map with strtolower
using array_map with strtolower on an array of characters. This method provides a clear and concise way to convert each character to lowercase.
Example:
<?php
// Original string
$str = "HELLO WORLD";
// Using array_map and strtolower
$array = str_split($str);
$lowerArray = array_map('strtolower', $array);
$final_str = implode('', $lowerArray);
echo $final_str;
?>
Output
hello world