Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

mb_strlen

Returns the number of characters in a string.

Available since: PHP 4

Syntax

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

Parameters

ParameterTypeDescription
$stringstringThe string to measure.
$encoding?stringThe encoding. Defaults to the internal encoding, usually UTF-8.

Return value

intThe number of characters in the string.

Examples

Length is counted in characters whatever the language:

<?php
 
echo mb_strlen('привет'), "\n";
echo mb_strlen('hello');

Output:

6
5

Checking a length limit:

<?php
 
$name = 'Александр';
 
var_dump(mb_strlen($name) <= 10);

Output:

bool(true)

Notes

Use mb_strlen() for any user-entered text: plain strlen() returns bytes and counts Cyrillic text as twice as long.

See also

  • strlen — Returns the length of a string in bytes.
  • mb_substr — Returns part of a string.

Related recipes

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.