PHP mb_strtolower: How to Handle Multibyte Strings Properly

php mb_strtolower function

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 using mb_internal_encoding().

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 fread Function: How to Read Files in PHP

In some cases, you need to handle a lot of data or simply try to open a file, or read…

Constants in PHP: How They Work & Examples

Constants in PHP store fixed values that don’t change during execution. We will cover the following topics in this article:…

Polymorphism in PHP: How It Works with Examples

PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…

PHP Global Variables & Superglobals: A Complete Guide

Variables in PHP are general elements used for data storage and other manipulations. Global variables belong to a particular category…

PHP sizeof: How to Count Elements in Arrays with Examples

The sizeof function in PHP exists to help developers know how many items an array holds. Understand the sizeof Function…

PHP While Loop: Iterating Through Code Blocks

The PHP “while” loop is a simple tool that repeatedly runs a block of code as long as a certain…

PHP MySQL LIMIT Data: How to Optimize PHP Queries?

There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…

PHP Predefined Constants Explained Simply

PHP has a number of predefined constants that give you some information about the language and its environment. You can…

MongoDB PHP Driver: Install and Get Started

Traditional relational databases like MySQL often take center stage. However, with the rise of NoSQL databases, MongoDB has emerged as…

Understanding the Increment and decrement operators in PHP

The increment and decrement are operators that can be used to increase or decrease the variable values which have a…

Previous Article

PHP array_map Function: How to Transform Arrays with Examples

Next Article

PHP explode Function: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.