PHP – Get the string length using mb_strlen()


In PHP, multibyte string length (mb_strlen) function is used to get the total string length of a specified string. This function is supported in PHP 4.6.0 or higher versions.

Syntax

int mb_strlen(str $string, str $encoding)

Parameters

mb_strlen() accepts two parameters: $string and $encoding.

  • $string− It is used to check the string length

  • $encoding− This parameter is used for character encoding. If it is omitted or null, then the internal character encoding value will be used.

Return Values

mb_strlen() returns the number of characters present in the given string. One is counted as a multi-byte character.

Errors/Exceptions

If the encoding is not known, then it will generate the level E_warning.

Example

 Live Demo

<?php
   // It will return total number of character
   $result = mb_strlen("Hello World","UTF-8");
   echo "Total number of characters: ", $result;
?>

Output

Total number of characters: 11

Note: that it counts the space too in the given string.

Updated on: 23-Aug-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements