Backend Development 5 min read

Using PHP mb_strlen() to Measure Multibyte String Length

This article explains how to use PHP's mb_strlen() function from the mbstring extension to accurately measure the length of multibyte strings, covering installation, syntax, optional encoding parameters, practical examples in Chinese and Japanese, and how to validate string length limits.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP mb_strlen() to Measure Multibyte String Length

In development we often need to handle multibyte strings such as Chinese or Japanese, and traditional PHP functions lack proper support. PHP provides the mb_strlen() function to obtain the length of multibyte strings.

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

The syntax of mb_strlen() is:

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

The parameter $str is the multibyte string whose length you want to calculate, and the optional $encoding specifies the character encoding. If $encoding is omitted, the default encoding returned by mb_internal_encoding() is used.

Below is a simple example that calculates the length of a Chinese string:

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

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

The function can also handle strings with a specified encoding. The following example uses a UTF‑8 encoded Japanese string:

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

Here the string consists of three Japanese characters and three Chinese characters, resulting in a length of 6 when encoded as UTF‑8.

Beyond measuring length, mb_strlen() can be used for length validation. The example below limits a string to a maximum of 20 characters:

<?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.";
}
?>

If the string exceeds the defined limit, the script outputs "String is too long."; otherwise it confirms the string is within the limit.

Through these examples we have learned the basic usage of mb_strlen() and common scenarios such as handling different encodings and validating string length. When dealing with multibyte strings in PHP, mb_strlen() provides a reliable way to obtain accurate lengths and enforce length constraints, improving program correctness and stability.

In summary, mb_strlen() is the PHP function for obtaining the length of multibyte strings. By specifying the appropriate character encoding, developers can flexibly handle various encodings, use the function for length checks, and enhance the accuracy and reliability of their applications.

ValidationPHPMultibytestring 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

login 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.