Home PHP mbstring PHP mbstring Definition
Definition and Usage The PHP mbstring extension (multibyte string) helps you work with text where one letter can use several bytes, such as é, ñ, 日 or an emoji. The normal string functions count bytes, so they can give wrong answers for this text.
Use mb_strlen() to count letters, mb_substr() to cut text, mb_strpos() to search, and mb_strtolower() or mb_strtoupper() to change case. mb_convert_encoding() changes the encoding of a string.
This page explains the ideas. The mbstring Functions index lists all 10 function guides.
Use UTF-8 everywhere. Save your files, database and pages as UTF-8, then pass "UTF-8" to the mb_* functions. That avoids most surprises.
Syntax
PHP Syntax The usual mbstring calls:
<?php
$text = "Héllo Wörld";
$length = mb_strlen($text, "UTF-8"); // 11
$part = mb_substr($text, 0, 5, "UTF-8"); // Héllo
$pos = mb_strpos($text, "ö", 0, "UTF-8"); // 7
$upper = mb_strtoupper($text, "UTF-8"); // HÉLLO WÖRLD
$utf8 = mb_convert_encoding($latin1, "UTF-8", "ISO-8859-1"); Reference
Core Ideas What to remember when you use mbstring:
Idea Example Remember Count mb_strlen($s, "UTF-8")Counts characters. strlen() counts bytes. Cut mb_substr($s, 0, 5, "UTF-8")Never splits a letter in half. Search mb_strpos($s, "x", 0, "UTF-8")Returns a character position or false. Case mb_strtolower($s, "UTF-8")Handles accents and other scripts. Convert mb_convert_encoding($s, "UTF-8", "ISO-8859-1")Target first, then the source encoding. Check mb_check_encoding($s, "UTF-8")Returns true if every byte is valid.
Code
PHP Examples Three short scripts that show how mbstring is used.
1. Count Characters 2. Cut Part of a String 3. Convert Latin-1 to UTF-8 Example 1 — Count Characters Count letters, not bytes.
<?php
$text = "Héllo";
echo strlen($text), PHP_EOL;
echo mb_strlen($text, "UTF-8"), PHP_EOL; How It Works The letter é is 2 bytes in UTF-8. strlen() returns 6, the number of bytes. mb_strlen() returns 5, the number of characters. Always pass the encoding to be safe. Example 2 — Cut Part of a String Take the first five characters safely.
<?php
$text = "Héllo Wörld";
echo mb_substr($text, 0, 5, "UTF-8"), PHP_EOL; How It Works Start at position 0. Take 5 characters. The accented letter stays whole. Use mb_substr() when you shorten text for a page. Example 3 — Convert Latin-1 to UTF-8 Change the encoding of an old string.
<?php
$latin1 = "caf\xE9";
$utf8 = mb_convert_encoding($latin1, "UTF-8", "ISO-8859-1");
echo $utf8, PHP_EOL; How It Works The byte \xE9 is the letter é in Latin-1. The target encoding is the second argument. The source encoding is the third argument. The result is valid UTF-8 text. Continue
Next: PHP mbstring Functions Index After you know the basics, open the function index for all 10 guides.
Full list: PHP mbstring Functions index (/php/mbstring/function).
Frequently Asked Questions What is the PHP mbstring extension? mbstring is a set of mb_* functions that work with multibyte text such as UTF-8. They count and cut by character instead of by byte.
Why does strlen() give the wrong length for my text? strlen() counts bytes. A letter such as é uses two bytes in UTF-8, so the byte count is larger than the letter count. Use mb_strlen() instead.
How do I convert Latin-1 text to UTF-8? Call mb_convert_encoding($text, "UTF-8", "ISO-8859-1"). The target encoding comes before the source encoding.
Do I need to enable mbstring? It is built into most PHP installs, but you may need to enable the mbstring extension in php.ini on some servers.
Which functions should I learn first? Start with mb_strlen(), mb_substr() and mb_strtolower(). They replace the byte-based functions you already know.
Where should I go next? Practice the examples here, then open the mbstring Functions index for all 10 function guides.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion