Current Location: Home> Function Categories> mb_strlen

mb_strlen

Get the string length
Name:mb_strlen
Category:Multi-byte string
Programming Language:php
One-line Description:Returns the length of the specified string in characters

Function name: mb_strlen()

Applicable version: PHP 4 >= 4.0.6, PHP 5, PHP 7

Function description: The mb_strlen() function returns the length of the specified string in characters. The mb_strlen() function is multibyte-safe.

Usage: mb_strlen(string $str, string|null $encoding = null): int

parameter:

  • $str: The string to calculate the length.
  • $encoding (optional): Specifies the character encoding of the string. If this parameter is not provided, internal character encoding is used.

Return value: Returns the length of the specified string in characters.

Example:

 $str = "Hello, 你好!"; $length = mb_strlen($str, "UTF-8"); echo "字符串的长度为:" . $length;

Output: The length of the string is: 10

Description: In the above example, we used the mb_strlen() function to calculate the length of the string $str. Since the string contains Chinese characters, we specify the character encoding as "UTF-8". The length returned by the function is 10 because "Hello, hello!" has 10 characters in total.

Similar Functions
Popular Articles