The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.
Table of Content
Understand the PHP mb_strtolower Function
The strtolower() only works with single-byte characters. If you use it on a UTF-8 string, it may break non-English letters. mb_strtolower() fixes that. It handles letters from many languages.
Here is the syntax:
mb_strtolower(string $string, ?string $encoding = null)
Parameters:
- $string
- The input string you want to convert to lowercase.
- It must be a multibyte-encoded string if you’re working with non-ASCII characters (e.g., UTF-8).
- Example:
"TÜRKÇE"
or"Пример"
.
- $encoding
- The character encoding of the string (e.g.,
"UTF-8"
,"ISO-8859-1"
). - If set to
null
, PHP uses the internal character encoding, which you can define usingmb_internal_encoding()
.
- The character encoding of the string (e.g.,
It returns the lowercase version of the input string with a string
type. It respects the character encoding.
Here is a quick example:
echo mb_strtolower("HELLO WORLD", 'UTF-8');
Here are the Use Cases:
- Normalize user input
- Handle email comparisons
- Create case-insensitive search
- Store lowercase data for multilingual apps
Languages like Turkish, Russian, and Chinese use multibyte characters. PHP does not handle them well without help. The mbstring extension helps with that. mb_strtolower() reads each character properly.
To lower UTF-8 text, pass the string and “UTF-8” as encoding:
$text = "GÜNAYDIN"; // Turkish for "Good Morning"
echo mb_strtolower($text, 'UTF-8');
The output:
günaydin
Here are key differences of mb_strtolower() vs strtolower() in PHP:
- strtolower() breaks with multibyte strings
- mb_strtolower() handles characters like ü, ğ, ş
- strtolower() works for basic English
- mb_strtolower() supports Unicode
Examples of mb_strtolower Function in PHP
Use mb_strtolower() with the right encoding:
$word = "Straße"; // German for "street"
echo mb_strtolower($word, 'UTF-8');
Output:
straße
Lowercase Turkish or German characters with PHP mb_strtolower():
Turkish uses letters like İ and ğ. German has ß. strtolower() does not convert these correctly. mb_strtolower() does:
echo mb_strtolower("İSTANBUL", 'UTF-8');
Output:
istanbul
Wrapping Up
In this article, you learned what the mb_strtolower()
function does and how it solves the problem of converting multibyte strings to lowercase in PHP. You also saw how it differs from strtolower()
and why it works better with non-English letters.
Here is a quick recap:
mb_strtolower()
turns every letter in a string to lowercase and works with multibyte encodings like UTF-8.- It is better than
strtolower()
when you deal with languages like Turkish, German, or Russian. - You must pass the right encoding to make it work as expected.
- It helps normalize user input, compare emails, build search features, and store data in lowercase.
- Always use it when handling non-ASCII text in PHP.
FAQs
Does mb_strtolower() support all languages?
It supports many but not all. It works well with most characters in Unicode.
How to change the default encoding for mb_strtolower()?
Use this:
mb_internal_encoding("UTF-8");
This sets UTF-8 for all mbstring functions.
What are common errors when you use mb_strtolower()?
You may forget to set encoding and may not install the mbstring extension. You may pass null or wrong type.
Can mb_strtolower() be used without the mbstring extension?
No. It comes from mbstring. If mbstring is missing, you get a fatal error.
How do I check if mbstring Is enabled in PHP?
Call:
phpinfo();
Or check:
extension_loaded('mbstring');
Similar Reads
The PHP is_readable helps you validate file readability before any operation like read or include files. What Is PHP is_readable? The is_readable() function…
PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…
PHP static method lets you call functions without an object. In this article, we will cover the following topics: The…
One of the important tasks is retrieving documents from a collection. MongoDB help us to make this process, but understanding…
The filter_var_array() appeared to make input validation simple and sanitization in PHP. It handles multiple inputs with different filters was…
A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…
In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…
There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…
PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values…
PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…