Master PHP’s mb_strlen: Handle Multibyte Strings with Ease

This article explains how to use PHP's mb_strlen function to accurately measure the length of multibyte strings such as Chinese or Japanese, covering installation of the mbstring extension, syntax, optional encoding, practical code examples, and common validation scenarios.

php Courses
php Courses
php Courses
Master PHP’s mb_strlen: Handle Multibyte Strings with Ease

In development we often need to handle multibyte strings such as Chinese or Japanese, and standard PHP functions lack proper support. PHP provides the mb_strlen() function to obtain the length of multibyte strings. This article introduces its usage with examples.

The mb_strlen() function is defined in the mbstring extension, so you must ensure the extension is installed and enabled. You can enable it by removing the comment in php.ini or checking with phpinfo().

Syntax of mb_strlen()

int mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )

The $str parameter is the multibyte string whose length you want to calculate. The optional $encoding parameter specifies the character encoding; if omitted, it defaults to the value returned by mb_internal_encoding().

Using mb_strlen()

Calculate the length of a Chinese string

<?php
$str = "你好,世界!";
echo mb_strlen($str); // Output: 7
?>

In this example the string contains four Chinese characters and three ASCII characters, so the length is 7.

Specify UTF‑8 encoding

<?php
$str = "こんにちは世界";
echo mb_strlen($str, "UTF-8"); // Output: 6
?>

Here the string has three Japanese characters and three Chinese characters, resulting in a length of 6.

Validate string length against a limit

<?php
$str = "This is a very long sentence.";
$max_length = 20;
if (mb_strlen($str) > $max_length) {
    echo "String is too long.";
} else {
    echo "String is within the limit.";
}
?>

This snippet shows how to enforce a maximum length using mb_strlen().

Overall, mb_strlen() is the PHP function for obtaining the length of multibyte strings. By specifying the encoding, you can flexibly handle strings in different encodings, making length checks and validations more reliable in backend development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPphp-functionsmultibytestring lengthmbstring
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.