PHP: Convert a string into lowercase.

This is a short guide on how to convert a string into lowercase using PHP. To do this, we can either use the function strtolower or the function 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:

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:

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 has been converted into a question mark inside a diamond. This is known as the 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.

So, how do we solve this? By using the mb_strtolower function instead!

Converting special characters into lowercase.

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

An example of mb_strtolower in use:

If you run the PHP snippet above, you will get 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:

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

Perfect! Hopefully, you found this tutorial useful!

Facebook Comments