PHP mbstring

Beginner
7 min read
Updated: Nov 2026
3 examples
UTF-8 text made easy

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.

PHP Syntax

The usual mbstring calls:

PHP
<?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");

Core Ideas

What to remember when you use mbstring:

IdeaExampleRemember
Countmb_strlen($s, "UTF-8")Counts characters. strlen() counts bytes.
Cutmb_substr($s, 0, 5, "UTF-8")Never splits a letter in half.
Searchmb_strpos($s, "x", 0, "UTF-8")Returns a character position or false.
Casemb_strtolower($s, "UTF-8")Handles accents and other scripts.
Convertmb_convert_encoding($s, "UTF-8", "ISO-8859-1")Target first, then the source encoding.
Checkmb_check_encoding($s, "UTF-8")Returns true if every byte is valid.

PHP Examples

Three short scripts that show how mbstring is used.

Example 1 — Count Characters

Count letters, not bytes.

PHP
<?php
$text = "Héllo";

echo strlen($text), PHP_EOL;
echo mb_strlen($text, "UTF-8"), PHP_EOL;

How It Works

  1. The letter é is 2 bytes in UTF-8.
  2. strlen() returns 6, the number of bytes.
  3. mb_strlen() returns 5, the number of characters.
  4. Always pass the encoding to be safe.

Example 2 — Cut Part of a String

Take the first five characters safely.

PHP
<?php
$text = "Héllo Wörld";

echo mb_substr($text, 0, 5, "UTF-8"), PHP_EOL;

How It Works

  1. Start at position 0.
  2. Take 5 characters.
  3. The accented letter stays whole.
  4. 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
<?php
$latin1 = "caf\xE9";

$utf8 = mb_convert_encoding($latin1, "UTF-8", "ISO-8859-1");

echo $utf8, PHP_EOL;

How It Works

  1. The byte \xE9 is the letter é in Latin-1.
  2. The target encoding is the second argument.
  3. The source encoding is the third argument.
  4. The result is valid UTF-8 text.

Next: PHP mbstring Functions Index

After you know the basics, open the function index for all 10 guides.

Start withWhy
mb_strlen()Count characters in a string
mb_substr()Cut part of a string by character
mb_strpos()Find the first match by character position
mb_strtolower()Make text lowercase
mb_convert_encoding()Convert between encodings
mb_check_encoding()Check that text is valid

Full list: PHP mbstring Functions index (/php/mbstring/function).

Frequently Asked Questions

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.
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.
Call mb_convert_encoding($text, "UTF-8", "ISO-8859-1"). The target encoding comes before the source encoding.
It is built into most PHP installs, but you may need to enable the mbstring extension in php.ini on some servers.
Start with mb_strlen(), mb_substr() and mb_strtolower(). They replace the byte-based functions you already know.
Practice the examples here, then open the mbstring Functions index for all 10 function guides.

Browse every mbstring function

Open the functions index for categorized guides with examples and FAQs.

mbstring Functions Index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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