PHP mb_strlen()Functions

PHP String 参考手册PHP String Reference Manual

Examples

The function returns the length of the string "Rookie Tutorial EXAMPLE":

<?php //Using mb_strlen echo mb_strlen("Rookie Tutorial EXAMPLE"); echo PHP_EOL; //Newline character //Using strlen echo strlen("Rookie Tutorial EXAMPLE"); ?>

The output result is:

10
18

Definition and Usage

The mb_strlen() function returns the length of a string. Unlikestrlenstrlen, it can return the corresponding number of characters by setting the character encoding, effectively handling the length issue of Chinese strings.

To use mb_strlen, you need to enable the mbstring extension.


Syntax

mb_strlen(string $string, ?string $encoding = null): int

mb_strlen() is used to set the string length.


Parameter Description
string Required. Specifies the string to be checked.
encoding Optional. The character encoding. If omitted, the internal character encoding is used.

Technical Details

Return value: Returns the length of the string on success, or 0 if the string is empty.
PHP version: 4+


PHP String 参考手册PHP String Reference Manual Other Extensions