(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_strlenRetourne la taille d'une chaîne


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

Récupère la taille de la chaîne fournie.

Liste de paramètres


La chaîne à analyser.


Le paramètre encoding est l'encodage des caractères. S'il est omis ou null, l'encodage de caractères interne sera utilisé.

Valeurs de retour

Retourne le nombre de caractères dans la chaîne string, avec l'encodage encoding. Un caractère multi-octets est alors compté pour 1.

Erreurs / Exceptions

Si l'encodage est inconnue, une erreur de niveau E_WARNING est générée.


Version Description
8.0.0 encoding est désormais nullable.

Voir aussi

add a note add a note

User Contributed Notes 7 notes

Yzmir Ramirez
10 years ago

14 years ago
Speed of mb_strlen varies a lot according to specified character set.

koala at example dot com
14 years ago


just be cautious when using mb_ functions in high frequented scripts.

# test runs: 1000000
# benchmarking strlen vs. mb_strlen
# mb_strlen: 5.5934538841248 ms, average: 5.5934538841248E-6 ms
ok 1 - mb_strlen is slower than strlen
# mb_strlen is 1.52 slower than strlen
# benchmarking strpos vs. mb_strpos
# mb_strlen: 31.180974960327 ms, average: 3.1180974960327E-5 ms
ok 2 - mb_strlen is slower than strlen
# mb_strpos is 5.62 slower than strpos
# benchmarking substr vs. mb_substr
# mb_strlen: 3.5374181270599 ms, average: 3.5374181270599E-6 ms
ok 3 - mb_strlen is slower than strlen
# mb_substr is 1.03 slower than substr
# benchmarking strtolower vs. mb_strtolower
# mb_strlen: 193.44901108742 ms, average: 0.00019344901108742 ms
ok 4 - mb_strlen is slower than strlen
# mb_strtolower is 43.5 slower than strtolower
# benchmarking strtoupper vs. mb_strtoupper
# mb_strlen: 340.71775603294 ms, average: 0.00034071775603294 ms
ok 5 - mb_strlen is slower than strlen
# mb_strtoupper is 112.78 slower than strtoupper
13 years ago


David Spector
1 year ago
It may not be clear whether PHP actually supports utf-8, which is the current de facto standard character encoding for Web documents, which supports most human languages. The good news is: it does.

I wrote a test program which successfully reads in a utf-8 file (without BOM) and manipulates the characters using mb_substr, mb_strlen, and mb_strpos (mb_substr should normally be avoided, as it must always start its search at character position 0).

The results with a variety of Unicode test characters in utf-8 encoding, up to four bytes in length, were mostly correct, except that accent marks were always mistakenly treated as separate characters instead of being combined with the previous character; this problem can be worked around by programming, when necessary.
motin at demomusic dot nu
14 years ago
Thank you Peter Albertsson for presenting that!



Two examples (from the two bugs found earlier):

1. Writing a string to a file:

if (
$fp) {
$len = strlen($str);
fwrite($fp, $str, $len);


2. Iterating through a string's characters:

$newStr = "";
for (
$newStr .= $str[$i];


filatei at gmail dot com
15 years ago


To Top