Table of Content
The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.
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
PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…
PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…
PHP array_diff_key compares arrays and removes elements that share the same key. It checks only keys and keeps the values…
PHP type juggling refers to the dynamic system where the type of a variable is determined by its context in…
Updating documents in MongoDB with PHP involves using the updateOne or updateMany methods from the MongoDB PHP library. These methods allow for precise updates,…
PHP array_intersect_assoc compares values and keys in arrays. It returns matches with the same key and value from all arrays…
PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…
PHP supports OOP with access modifiers to prevent unintended changes and improve security. In this article, we will cover the…
A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…
Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…