Using PHP mb_strlen() to Measure Multibyte String Length
This article explains how the PHP mb_strlen() function from the mbstring extension can accurately calculate the length of multibyte strings such as Chinese or Japanese, shows its syntax, optional encoding parameter, and provides practical code examples for length checking 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 related comment in the php.ini file or by checking the current PHP configuration with the phpinfo() function.
mb_strlen() function syntax
<code>int mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )</code>In this signature, $str is the multibyte string whose length you want to calculate, and $encoding is an optional parameter that specifies the character encoding of the string. If $encoding is omitted, the function uses the encoding returned by mb_internal_encoding() by default.
mb_strlen() function usage
Using mb_strlen() to calculate the length of a Chinese string:
<code><?php
$str = "你好,世界!";
echo mb_strlen($str); // Output: 7
?></code>In the above example, mb_strlen() calculates the length of $str . The string contains four Chinese characters and three ASCII characters, so the result is 7.
Using a UTF-8 encoded string:
The mb_strlen() function can also specify a character encoding when processing strings of different encodings.
<code><?php
$str = "こんにちは世界";
echo mb_strlen($str, "UTF-8"); // Output: 6
?></code>Here the encoding is set to UTF‑8, and mb_strlen() returns 6 because the string contains three Japanese characters and three Chinese characters.
mb_strlen() function to validate string length
For example, you can restrict a string to a maximum length. The following code demonstrates this:
<code><?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.";
}
?></code>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 learn the basic usage of mb_strlen() and common scenarios such as length checking and validation. When dealing with multibyte strings in real projects, mb_strlen() helps you handle them more conveniently and accurately.
The mb_strlen() function is a PHP tool for obtaining the length of multibyte strings. By specifying the character encoding, you can flexibly process strings of different encodings, use the function for length verification, 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.