Content · Programming · PHP

PHP mb_strlen() Function: Usage and Examples

Learn how to count characters in multibyte text in PHP with syntax, clear code examples, practical use, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When working with PHP, you may need to count characters in multibyte text. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.

mb_strlen: UTF-8 characters can occupy several bytes, so strlen() and mb_strlen() can return different values.

What Is mb_strlen in PHP?

Count characters in multibyte text. UTF-8 characters can occupy several bytes, so strlen() and mb_strlen() can return different values.

Syntax:

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

Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.

Important: A code-point count is not always a count of visible characters; combining marks affect the distinction.

Example 1: Basic Usage

This example introduces the operation with a short and predictable input.

<?php

echo mb_strlen("Café", "UTF-8");

The code focuses on the main operation. UTF-8 characters can occupy several bytes, so strlen() and mb_strlen() can return different values.

Example 2: Another Case

Now compare a different input, locale, date, or option to see another part of the behavior.

<?php

echo strlen("Café") . " bytes";

Output:

5 bytes

What changes? A code-point count is not always a count of visible characters; combining marks affect the distinction.

Practical Example: Applying It in an Application

Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.

<?php

$names = ["Ana", "Björn", "Zoë"];
foreach ($names as $name) {
    echo $name . ": " . mb_strlen($name, "UTF-8") . " characters" . PHP_EOL;
}

The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. UTF-8 characters can occupy several bytes, so strlen() and mb_strlen() can return different values.

Important Behavior and Edge Cases

  • Core behavior: UTF-8 characters can occupy several bytes, so strlen() and mb_strlen() can return different values.
  • Inputs and results: A code-point count is not always a count of visible characters; combining marks affect the distinction.
  • Alternatives: strlen() counts bytes and grapheme_strlen() counts grapheme clusters.
  • Compatibility: mbstring is required for mb_* functions.

Comparison With Related Functions

Function or ApproachWhen to Use It
mb_strlenCount characters in multibyte text.
Related PHP approachstrlen() counts bytes and grapheme_strlen() counts grapheme clusters.

Common Beginner Mistakes

MistakeCorrect Approach
Using an incorrect input or optionA code-point count is not always a count of visible characters; combining marks affect the distinction.
Confusing this API with a related operationstrlen() counts bytes and grapheme_strlen() counts grapheme clusters.
Assuming output is identical across environmentsCheck return values and required extensions or versions. mbstring is required for mb_* functions.

Key Takeaways

  • mb_strlen helps you count characters in multibyte text.
  • UTF-8 characters can occupy several bytes, so strlen() and mb_strlen() can return different values.
  • A code-point count is not always a count of visible characters; combining marks affect the distinction.

Use mb_strlen when you need to count characters in multibyte text. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.

Share this resourceSend this page to someone who may find it useful.