PHP: Convert a string into lowercase.

In this tutorial, we are going to show you how to convert a string into lowercase using PHP.

To do this, we can either use the function strtolower or  mb_strtolower.

Using the strtolower function.

Using the strtolower function is pretty straight-forward. Simply pass your string into the function and it will return that string in a lowercase format.

//Example PHP string
$example = 'ThIs Is A tEsT';

//Pass it into strtolower
$example = strtolower($example);

//var_dump it out
var_dump($example); //result: this is a test

As you can see, the strtolower function is pretty simple to use.

However, there is one issue with it: It cannot convert special characters.

Any special or accented characters will be converted into garbled replacement characters.

Take a look at following example:

//A PHP string with special
//characters in it
$string = 'U-umlaut: Ü';

//Pass the string into strtolower
$string = strtolower($string);

//Dump it out
var_dump($string);

In the code above, we attempted to convert an U-umlaut character into lowercase. However, this will fail, as strtolower is unable to convert special characters like the U-umlaut or the A-umlaut.

As a result, we end up with a string that looks like this.

u-umlaut

As you can see, our special character looks like a question mark inside a diamond. This “diamond” is a replacement character.

When you see this character, it means that the system was unable to find the correct symbol. In this case, strtolower could not find the correct lowercase symbol for our U-umlaut character.

To solve this issue, we will need to use the mb_strtolower function instead.

Converting special characters into lowercase.

mb_strtolower is a multi-byte safe function that can convert special characters into lowercase. Basically, it can handle any special character that has an “alphabetic” property.

An example of mb_strtolower in use:

//A PHP string with an
//U-umlaut character
$string = 'U-umlaut: Ü';

//Pass the string into mb_strtolower
$string = mb_strtolower($string);

//Dump it out
var_dump($string);

If you run the PHP snippet above, you will see the following output:

mb_strtolower

As you can see in the screenshot above, our U-umlaut has been converted into its lowercase equivalent.

Here is another example.

//Multiple special characters
$string = 'ÄÜÉ';

//Use mb_strtolower
$string = mb_strtolower($string);

//Dump it out
var_dump($string);

In this case, we were able to convert multiple special characters into their lowercase equivalents.

Perfect! Hopefully, you found this tutorial useful!