Using PHP mb_strlen() to Get the Length of Multibyte Strings
This article explains how to enable the mbstring extension and use PHP's mb_strlen() function with optional encoding parameters to accurately measure the length of multibyte strings such as Chinese and Japanese, including practical code examples for length calculation and validation.
In development we often need to handle multibyte strings like Chinese, Japanese, etc., and traditional PHP functions do not support them well. Therefore PHP provides the mb_strlen() function to obtain the length of multibyte strings. This article introduces the usage of mb_strlen() and provides several code examples.
The mb_strlen() function is defined in the mbstring extension, so before using it you must ensure that the mbstring extension is installed and enabled. You can enable it by removing the comment in the php.ini file or verify it with the phpinfo() function.
The syntax of mb_strlen() is as follows:
int mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )The parameter $str is the multibyte string whose length you want to calculate. The optional parameter $encoding specifies the character encoding; if omitted, the default encoding returned by mb_internal_encoding() is used.
Below is a simple example that uses mb_strlen() to 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 function returns 7.
The mb_strlen() function can also specify the character encoding when handling strings of different encodings. The following example uses a UTF‑8 encoded Japanese string:
<?php
$str = "こんにちは世界";
echo mb_strlen($str, "UTF-8"); // Output: 6
?>Here the string contains three Japanese characters and three Chinese characters, resulting in a length of 6.
Besides obtaining the length, mb_strlen() can be used to validate whether a string meets length requirements. 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 length of $str exceeds the limit, the script outputs "String is too long."; otherwise it outputs "String is within the limit.".
Through these examples we have learned the basic usage of mb_strlen() and common scenarios such as length calculation and validation. When dealing with multibyte strings in real projects, mb_strlen() helps handle them more conveniently and accurately.
Summary : The mb_strlen() function is a PHP tool for obtaining the length of multibyte strings. By specifying the character encoding, you can flexibly handle strings of different encodings, use the function for length checks, and improve the accuracy and stability of your programs.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.