PHP 8 reference · Multibyte String Functions

PHP mb_strlen() Function

Return the number of characters in a string, counting é or 日 as one character. The UTF-8-safe version of strlen().

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
string The input string.
encoding optional The character encoding; defaults to mb_internal_encoding() (UTF-8).

Return value

The number of characters.

Examples

Characters, not bytesTry it yourself
<?php
$word = "naïve café";
echo strlen($word), "\n";    // bytes
echo mb_strlen($word), "\n"; // characters

Output

12
10
Validate a length limitTry it yourself
<?php
$bio = "Ich heiße Jürgen und wohne in Köln.";
echo mb_strlen($bio) <= 40 ? "fits" : "too long", "\n";

Output

fits

Use mb_strlen() whenever a length limit is shown to people, such as a 40-character bio: users count characters, not bytes.

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?