PHP mb_strlen() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
mbstring

Definition

In PHP, mb_strlen() returns the number of characters in a string, using a character encoding you can choose.

Unlike strlen(), it counts a letter such as é or a symbol such as 日 as one character.

Syntax

Call mb_strlen() with the values you need. Start with this line.

PHP
$count = mb_strlen($text, "UTF-8");
CallWhat it does
mb_strlen("Héllo", "UTF-8")Returns 5.
mb_strlen("日本語", "UTF-8")Returns 3.

If you leave out the encoding, the internal encoding is used. It is UTF-8 on most servers.

Library declarations
PHP
mb_strlen(string $string, ?string $encoding = null): int

Parameters

ParameterDescription
stringRequired. The text to measure.
encodingOptional. The character encoding. Default is the internal encoding.

Return Value

Return ValueDescription
intThe number of characters in the string.

PHP Examples

Example 1 — Count Characters

Compare mb_strlen() with strlen().

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

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

How It Works

  1. Use a word with an accent. The letter é is 2 bytes.
  2. strlen() counts bytes, so it returns 6.
  3. mb_strlen() counts characters, so it returns 5.
  4. Pass UTF-8 so the result does not depend on settings.

Example 2 — Count Other Scripts

Japanese letters use 3 bytes each in UTF-8.

PHP
<?php
$text = "日本語";

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

How It Works

  1. The text has three letters.
  2. Each letter takes 3 bytes in UTF-8, so strlen() gives 9.
  3. mb_strlen() gives 3, the number of letters.
  4. The same is true for most non-Latin text.

Example 3 — Count Emoji and Check a Limit

An emoji is one character, but 4 bytes.

PHP
<?php
$post = "Hi 👍";

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

if (mb_strlen($post, "UTF-8") <= 10) {
    echo "Short enough", PHP_EOL;
}

How It Works

  1. The text has 4 characters: H, i, a space and the emoji.
  2. strlen() returns 7 because the emoji uses 4 bytes.
  3. mb_strlen() returns 4.
  4. Use the character count for limits on posts and names.

Frequently Asked Questions

mb_strlen() returns the number of characters in a string, based on a character encoding.
mb_strlen(string $string, ?string $encoding = null): int.
strlen() counts bytes. mb_strlen() counts characters, so é and 日 each count as one.
It counts each emoji code point as one character. A few emoji are made of several code points and count as more than one.
Yes. mb_strlen() is only available if the mbstring extension is enabled.

Next: mb_strpos()

mb_strpos() finds the character position of the first match in a string.

mb_strpos() tutorial →

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