PHP String Length: Using mb_strlen()

In PHP, use the mb_strlen() function to calculate the character length of a string. This function calculates the length based on the actual character encoding of the string. Example:

$str = "Hello World";
$length = mb_strlen($str);
echo $length; // 输出:11

To specify a character encoding, you can pass a second parameter in the function.

$str = "你好,世界";
$length = mb_strlen($str, 'UTF-8');
echo $length; // 输出:7

Please note, when using the mb_strlen() function, make sure the mbstring extension is enabled in the PHP configuration.

bannerAds