PHP mb_strlen() Function

What is PHP mb_strlen() function?

If you want to correctly know the character number a string, use mb_strlen() function (Check example 1). This function can count the true character number in a string no matter whether the string has single byte or multi-byte characters. This function also counts the length of whitespace, special characters (Check example 2).

Most of us uses UTF-8 character encoding system in our web application and It supports multi-byte character set. A UTF-8 character can consist 1 to 4 bytes. The UTF-8 character encoding system is ASCII compactable also which is a single-byte character encoding system. You can find the default character encoding system in php.ini file in “default_charset = “ setting.

Syntax:

mb_strlen(string, encoding)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

string (required): The string to check the length for.

encoding (optional): The character encoding system to use. It will be used to encode the string. If you omit this parameter or use NULL as value, the default character encoding system will be used. But, the default encoding may not encode the string properly. Check exercise 3.

Return Values:

It returns the number of characters in the provided string. As it counts character number, so, no matter if a character is built with 2 or 4 bytes, the function counts the character as 1.

Examples:

Example 1: Finding length of a string-

<?php
$string = "SchoolsOfWeb";
echo "The length of the string '$string' is: ".mb_strlen($string);
?>

Output:

The length of the string ‘SchoolsOfWeb’ is: 12

Explanation:

The function tells us that the string has 12 characters. As there is apparently 12 characters and the function returns 12, we can safely guess that all characters are 1-byte long.

Example 2: Finding length that consists special characters-

<?php  // EXAMPLE 2
$string = "Schools Of Web &>";
echo "The length of the string '$string' is: ".mb_strlen($string);
?>

Output:

The length of the string ‘Schools Of Web &>’ is: 17

Explanation:

The function tells us that the string has 17 characters. As there is apparently 17 characters and the returns 17, we can safely guess that all characters are 1-byte long. Note, the string also consists of whitespaces and special characters and the function also counts those.

Example 3: Finding length that consists multi-byte characters-

<?php  // EXAMPLE 3
$string = "£©€";
echo "The length of the string '$string' is: ".mb_strlen($string);
echo "<br />";
$string = "いらっしゃいませ";
echo "The length of the string '$string' is: ".mb_strlen($string, "UTF-8");
?>

Output:

The length of the string ‘£©€’ is: 3
The length of the string ‘いらっしゃいませ’ is: 8

Explanation:

The function tells us that the string ‘£©€’ has 3 characters. Though, apparently, there are 3 characters, but, these are multi-byte characters. The £ character is 3-byte and the remaining two characters are 2-byte long. As a result, the byte number and character number are nit same here.

The string ‘いらっしゃいませ’ has 8 characters though these are multi-byte characters.

Example 4: Finding length with character encoding-

<?php
$string = "SchoolsOfWeb";
echo "The length of the string '$string' is: ".mb_strlen($string, "UTF-8");
echo "<br />";
echo "The length of the string '$string' is: ".mb_strlen($string);
?>

Output:

The length of the string ‘SchoolsOfWeb’ is: 12
The length of the string ‘SchoolsOfWeb’ is: 12

Explanation:

In line 3, the string is encoded with the “UTF-8” character encoding system. It’s a multi-byte character encoding system.

In line 5, the mb_strlen() function operate on the same string without, but, tough we don’t mention any character encoding system, the function uses the default encoding system defined in the php.ini file which is the same according to our system. Hence, both line 3 and line 5 output same.

Example 5: Finding length that consists escape sequence-

<pre>
<?php  // EXAMPLE 5
$string = "\t";
echo "The length of the string \\t is: ".mb_strlen($string);
?>
</pre>

Output:

The length of the string \t is: 1

Explanation:

Apparently, the string has 2 characters – “\” and “t”. But, the function considers it collectively one character. So, it returns 1.

Example 6: Finding length of an empty string-

<?php  // EXAMPLE 6
$string = "";
echo "The length of the string '$string' is: ".mb_strlen($string);
?>

Output:

The length of the string ” is: 0

Explanation:

As the empty string character, the function returns 0 as its length as expected.

Example 7: Finding length of a NULL variable-

<?php  // EXAMPLE 7
$string = NULL;
echo "The length of the NULL string is: ".mb_strlen($string);
?>

Output:

The length of the NULL string is: 0

Explanation:

Though the NULL variable’s length is not equal to 0, the function returns 0 unexpectedly.

Practical Usages of mb_strlen() Function:

  • To validate form inputs: Username could be 5-12 characters long. You can use this function to check whether provided username is within the range in the server side.
  • To validate database inputs: If a field’s length is smaller than the value you attempt to enter, you’ll get an incomplete value when you retrieve that value later. So, before inserting a value into a table, you check its length by mb_strlen() function.

Notes on mb_strlen() Function:

  • mb_strlen() function considers length of each special sequence character as one not two. A special sequence character starts with a backslash(\) followed by another character, example – \n, \t, \r etc. Check example 5.
  • mb_strlen() function doesn’t return expected value for NULL variable. It returns 0. Check example 7.
  • To accurately get character length in a string use mb_strlen() function instead of strlen() function. In case, all the characters in the string are single-byte, both function returns same, otherwise, strlen() returns incorrect character number. Because strlen() assumes each character is 1 byte long.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP mb_strlen() Function

To safely know the character length of a string, you can use PHP mb_strlen() function which is one of the multibyte string functions.

Reference:

https://www.php.net/manual/en/function.mb-strlen.php